記事ディレクトリ
フロントエンドの高速インストール
- 方法 1: Express が提供するスキャフォールディングを使用してアプリケーションのスケルトンを直接作成する
- スキャフォールディングをインストールする npm install -g Express-generator
- プロジェクトを作成します。
- 依存関係をインストールする npm install
- プロジェクトノード bin/www を開始します。
- 方法 2: 独自の高速アプリケーション構造を最初から構築します。
- プロジェクトを初期化する npm init
- Express npm i Express をインストールする
1. 基本的な使い方
- インポート –> 作成 –> リッスン
- リファレンスドキュメントを使用する
const express = require('express');
// * 创建express服务器
const app=express()
// * 启动服务器 ,监听端口
app.listen(8000,()=>{
console.log('启动express 服务器')
})
// 请求
app.post('/login',(req,res)=>{
res.end('登录成功')
})
app.get('/home',(req,res)=>{
res.end('home 列表模块')
})
2. ミドルウェア
- ミドルウェアの本質は、express に渡されるコールバック関数です。
- このコールバック関数は 3 つのパラメータを受け入れます。
- リクエストオブジェクト (リクエストオブジェクト);
- 応答オブジェクト (応答オブジェクト);
- next function (次のミドルウェアを実行するためにexpressで定義された関数);
重要
: ミドルウェアの実行処理では、最初に一致したミドルウェアのみが実行されますので、後から実行されるかどうかを確認してください。next
app.post('/login', (req, res, next) => {
// 中间件中的req与res可以进行修改
res.aaa = '添加aaa并修改res'
// 2. JSON结束
// res.json({message:'登录成功',code:200})
// res.end('登录成功')
// 3. next 匹配执行下一个中间件
next()
})
注意点
:現在のミドルウェア関数がリクエストとレスポンスのサイクルを終了しない場合は、next() を呼び出す必要があります。これにより制御が次のミドルウェア関数に渡され、そうでない場合、リクエストは一時停止されます。
const express = require('express');
// * 创建express服务器
const app = express()
// * 启动服务器 ,监听端口
app.listen(8000, () => {
console.log('启动express 服务器')
})
// 请求
app.post('/login', (req, res, next) => {
// 中间件中的req与res可以进行修改
res.aaa = '添加aaa并修改res'
// 2. JSON结束
// res.json({message:'登录成功',code:200})
// res.end('登录成功')
// 3. next 匹配执行下一个中间件
next()
})
app.use((req,res,next)=>{
console.log('执行下一个中间件next');
})
app.get('/home', (req, res) => {
res.end('home 列表模块')
})
2.1 ミドルウェアアプリケーション
- Express は主に次の 2 つの方法を提供します。
- app/router.use;
- app/router.methods;
アプリの使用
app.use((req,res,next)=>{
console.log('执行下一个中间件next');
})
アプリメソッド
app.get('/home', (req, res) => {
res.end('home 列表模块')
})
3. ミドルウェアの登録方法
3.1 共通ミドルウェアの登録
use注册
ミドルウェアはパスやリクエストに関係なくマッチングできます。- 同時に、マッチングプロセス中に
next()
、最初のミドルウェアのみが実行されます。
// 1. use注册的中间件不管什么路径或者请求都可以匹配的上
// 2. 同时在匹配的过程中如何不 next(), 只会执行第一个中间件
app.use((req,res,next)=>{
console.log('执行下一个中间件next');
next()
})
app.use(()=>{
console.log('执行第二个中间件')
})
3.2 パスマッチングミドルウェア
・パスマッチングミドルウェアはパスを限定するだけでリクエストメソッドは表示されません。
// 这里的路径匹配中间件只是对路径做限制并没有请求方式做显示
// 不管method如何都可以匹配
app.use('/home',(req,res,next)=>{
console.log('路径匹配中间件')
})
3.3 メソッドとパスのマッチング
- 文法:
app.method(path,middleware)
- 一致するミドルウェアは、要件を満たす最初のミドルウェアのみと一致します。次のミドルウェアが実行されるかどうかは、nextが呼び出されるかどうかによって決まります。
// app.method(path,middleware)
app.get('/home',(req,res,next)=>{
console.log('路径以及方法匹配');
res.end('匹配成功')
})
// 注册多个中间件
app.get('/home', (req, res, next) => {
console.log('路径以及方法匹配');
res.end('匹配成功')
// 中间的匹配只会匹配第一个符合要求的中间件 , 关于下一个中间件是否执行看有没有调用next
}, (req, res, next)=>{
console.log('关于这个中间件的执行需要看上一个中间件是否有next');
})
3.4 ケースミドルウェアのマッチングと実行方法
- 通常の直接書き込み
app.post('/login', (req, res, next) => {
req.on('data', data => {
let userInfo = data.toString()
const user = JSON.parse(userInfo)
if (user.username === 'admin' && user.password===123456) {
res.end('登录成功')
}else{
res.end('账号或者密码错误')
}
})
})
// 注册信息
app.post('/register', (req, res, next) => {
// res.end('注册成功')
// 注册要查询数据库,看是否存在用户名
if (true) {
req.on('data', data => {
let userInfo = data.toString()
const user = JSON.parse(userInfo)
if (user.username === 'admin' && user.password === 123456) {
res.end('注册成功')
} else {
res.end('账号或者密码错误')
}
})
}
})
- **最適化
JSON解析,放到body后next()
**
// 1. JSON解析,放到body
app.use((req, res, next) => {
if (req.headers['content-type'] === 'application/json') {
req.on('data', data => {
const jsonInfo = JSON.parse(data.toString())
req.body = jsonInfo
})
req.on('end', () => {
next()
})
}
})
// 账号密码
app.post('/login', (req, res, next) => {
console.log(req.body);
res.end('登录成功')
})
// 注册信息
app.post('/register', (req, res, next) => {
console.log(req.body);
})
4. ミドルウェアリクエストデータの解析
express
リクエストの解析を完了するのに役立つ組み込みミドルウェアがいくつかあります。
4.1 リクエストボディの解析ミドルウェア
app.use((req, res, next) => {
if (req.headers['content-type'] === 'application/json') {
req.on('data', data => {
const jsonInfo = JSON.parse(data.toString())
req.body = jsonInfo
})
req.on('end', () => {
next()
})
}
next()
})
// 账号密码
app.post('/login', (req, res, next) => {
console.log(req.body);
res.end('登录成功')
})
- 上記のコードで使用されています
JSON.parse对data数据进行解析
が、express中提供了 json可以直接进行解析
app.use(express.json())
// 账号密码
app.post('/login', (req, res, next) => {
console.log(req.body);
res.end('登录成功')
})
4.2 urlencoded解析
- クライアントの解析では
urlencoded
渡されたパラメータを使用するため、次を使用する必要があります。express.urlencoded()
app.use(express.urlencoded()) // 解析客户端利用urlencoded传参
// 解决 body - parser deprecated undefined extended: provide extended option 05urlencoded警告
app.use(express.urlencoded({
extended: true }))
app.post('/login', (req, res, next) => {
console.log(req.body);
res.end('登录成功')
})
5. サードパーティのミドルウェア
5.1 モルガン伐採
const express = require('express');
const morgan = require('morgan');
const fs = require('fs');
const app = express()
// cnpm i morgan 安装
// 第三方中间件 合并日志
const writerLog=fs.createWriteStream('./log.txt')
// 日志写入
app.use(morgan('combined', {
stream: writerLog }))
app.post('/login', (req, res, next) => {
console.log(req.body);
res.end('登录成功')
})
5.2 マルチファイルのアップロード
- インストール
npm i multer --save
Multer
multipart/form-data
あらゆる種類のフォーム データのみを処理します。Multer
express 的 request 对象里添加一个 body 对象
これは(フォームのテキスト フィールド情報を含む) とfile
orfiles
オブジェクト (単一のファイルはreq.file
取得を通じて取得され、複数のファイルは取得を通じて取得されreq.files
、ファイル オブジェクトまたはファイル オブジェクトにはオブジェクト フォームによってアップロードされたファイル情報が含まれます)に含まれます。注意点
Upload.single の値はフロントエンド名の値に対応し、同時にform-data
キーの値も同じに保つ必要があります。
const multer = require('multer');
// 对上传的文件名字重起
const upload = multer({
storage: multer.diskStorage({
// 文件名称,
// destination 是用来确定上传的文件应该存储在哪个文件夹中
// destination 是一个函数,必须创建这个文件夹
destination(require, file, callback) {
callback(null, 'uploads/')
},
filename(require, file, callback) {
// originalname是文件上传时的名字,可以根据它获取后缀
callback(null, Date.now() + '_' + file.originalname)
}
})
})
app.post('/upload', upload.single("file"), (req, res, next) => {
console.log(req.file); // 文件信息
res.end('文件上传成功')
})
- 複数のファイルのアップロード、配列を受け入れます
multer实例.array(fielname[,maxCount])
to を受信している間fielname命名的文件数组
;maxCount——限制上传的最大数量,这些文件的信息保存在req.files里面
app.post('/upload', upload.array("file"), (req, res, next) => {
console.log(req.files); // 文件信息
res.end('文件上传成功')
})
参照分析
フォームデータ内の通常のデータを解析する
const formData= multer()
app.post('/login',formData.any(), (req, res, next) => {
console.log(req.body);
res.end('登录成功')
})
6.パラメータ分析paramsとクエリ
- クエリは主にページングに使用されます
app.post('/list', (req, res, next) => {
// http://localhost:8000/list?offset=10&page=20解析
console.log(req.query); // { offset: '10', page: '20' }
res.end('登录成功')
})
- params は主に ID を渡すために使用されます
app.post('/user/:id', (req, res, next) => {
// http://localhost:8000/user/1100
const id = req.params.id
res.end(`获取用户${
id}`)
})
7. 応答データ
end方式
: http の response.end メソッドと同様に、使用方法は一貫しています。json方法
: オブジェクト、配列、文字列、ブール値、数値、null など、多くの型を json メソッドに渡すことができ、それらは json 形式に変換されて返されます。status方法
: ステータスコードを設定するために使用される注意
関数です。- その他の応答コンテンツの参照
app.get('/login', (req, res, next) => {
// 1. 方法一 end
// res.end(`响应数据---登录成功`)
// 2. JSON数据响应
// res.json({
// code: 0, message: '欢迎回来', data: [{
// name: "admin",
// avator:'www.http.ccc.jpg'
// }]
// })
// 3. status 方法,设置http状态码
res.status(201)
res.json(
{
code: 0, message: '欢迎回来', data: [{
name: "admin",
avator: 'www.http.ccc.jpg'
}]
}
)
})
8. ルーティング
express.Router
ルートハンドラーを作成するには
useRouter.js
const express = require('express');
const userRouter = express.Router()
userRouter.get('/', (req, res, next) => {
res.json(
{
code: 0, message: 'success', data: [{
name: "admin",
password:'123456'
}, {
name: "admin",
password: '123456'
}]
}
)
})
userRouter.get('/:id',(req, res, next) => {
const id=req.params.id
res.end(id)
})
userRouter.post('/', (req, res, next) => {
})
userRouter.delete('/:id', (req, res, next) => {
})
userRouter.patch('/:id', (req, res, next) => {
})
module.exports=userRouter
インデックス.js
const userRouter = require('./router/userRouters.js');
app.use('/users', userRouter)
9. 静的リソース
express 内置static() 静态资源
、フォルダーを直接静的リソースにします
app.use(express.static('./uploads'))
10 エラー処理
- 通常のエラー処理ではif判定処理用のフロントエンドデータを使用する必要があります。
- ただし、各インターフェースは判定を書き込んだり、同じエラーメッセージやステータスコードを返したりするため、コードに冗長性が生じます。
app.post('/login', (req, res, next) => {
const {
username, password } = req.body
if (username !== 'admin' || password !== 123456) {
res.json({
code: 1002,
message: '账号密码错误'
})
} else {
console.log('xx')
res.json({
code: 200,
message: '登录成功'
})
}
})
- シンプルなカプセル化と統一処理
// 中间件处理错误信息
app.post('/login', (req, res, next) => {
if (username !== 'admin' || password !== 123456) {
next(1002)
}
else {
next()
}
})
app.use((errCode, req, res, next) => {
const code = errCode
let message = ''
switch (code) {
case 1001:
message = '未知的错误'
break
case 1002:
message = '账号或密码错误'
default:
message = '登录成功'
}
res.json({
code: errCode,
message,
})
})