Node.js framework express.js middleware All In One
Node.js framework express.js middleware All In One
express.js middlewares order
writing middleware
const express = require('express')
const app = express()
const myLogger = function (req, res, next) {
console.log('LOGGED')
next()
}
app.use(myLogger)
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(3000)
https://expressjs.com/en/guide/writing-middleware.html
demos
// const express = require('express')
import express from 'express';
const app = express()
const middleware1 = function (req, res, next) {
console.log(`middleware 1 ✅`)
next()
}
const middleware2 = (req, res, next) => {
console.log(`middleware 2 ✅✅`)
next()
}
const middleware3 = (req, res, next) => {
console.log(`middleware 3 ✅✅✅`)
next()
}
app.use(middleware1)
app.use(middleware2)
app.use(middleware3)
app.get('/', (req, res) => {
res.send('Hello World!');
})
// app.listen(3000)
const PORT = 3000;
app.listen(PORT, () => {
console.log(`server is running on http://localhost:${PORT}`);
})
// const express = require('express')
import express from 'express';
const app = express()
const middleware1 = function (req, res, next) {
console.log(`middleware 1 ✅`)
next()
}
const middleware2 = (req, res, next) => {
console.log(`middleware 2 ✅✅`)
next()
}
const middleware3 = (req, res, next) => {
console.log(`middleware 3 ✅✅✅`)
next()
}
app.use(middleware1, middleware2, middleware3);
app.get('/', (req, res) => {
res.send('Hello World!');
})
// app.listen(3000)
const PORT = 3000;
app.listen(PORT, () => {
console.log(`server is running on http://localhost:${PORT}`);
})
// const express = require('express')
// const bodyParser = require('body-parser');
import express from 'express';
import bodyParser from 'body-parser';
import router from './routers/index.js';
const app = express()
// ❌
app.use(router)
// get POST request's body
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
// // ✅
// app.use(router)
const auth = (req, res, next) => {
console.log(`req.originalUrl`, req.originalUrl);
if(req.originalUrl) {
next()
// ⚠️
return;
}
res.send(`403 error`);
}
const middleware1 = function (req, res, next) {
console.log(`middleware 1 ✅ before`)
next()
console.log(`middleware 1 ✅ after`)
}
const middleware2 = (req, res, next) => {
console.log(`middleware 2 ✅✅ before`)
next()
console.log(`middleware 2 ✅✅ after`)
}
const middleware3 = (req, res, next) => {
console.log(`middleware 3 ✅✅✅ before`)
next()
console.log(`middleware 3 ✅✅✅ after`)
}
app.use(middleware1, middleware2, middleware3);
app.get('/', auth, (req, res) => {
res.send('Hello World!');
})
// app.listen(3000)
const PORT = 3000;
app.listen(PORT, () => {
console.log(`server is running on http://localhost:${PORT}`);
})
// http://localhost:3000
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
video tutorials
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17823528.html
未经授权禁止转载,违者必究!