赞
踩
const express = require('express') // 加一个注释,用以说明,本项目代码可以任意定制更改 const app = express() const PORT = process.env.PORT || 3000 // // 挂载路由 // app.use('/api', router) // // 挂载统一处理服务端错误中间件 // app.use(errorHandler()) app.get('/', (req, res) => { console.log(`${req.method}, ${req.url}, ${Date.now()}`); res.send('/index') }) app.get('/register', (req, res) => { console.log(`${req.method}, ${req.url}, ${Date.now()}`); res.send('/iregisterdex') }) app.get('/login', (req, res) => { console.log(`${req.method}, ${req.url}, ${Date.now()}`); res.send('/login') }) app.listen(PORT, () => { console.log(`Server is running at http://localhost:${PORT}`) })
封装个方法:
const express = require('express') // 加一个注释,用以说明,本项目代码可以任意定制更改 const app = express() const PORT = process.env.PORT || 3000 function logs(req) { console.log(`${req.method}, ${req.url}, ${Date.now()}`); console.log(`${req.method}, ${req.url}, ${Date.now()}`); console.log(`${req.method}, ${req.url}, ${Date.now()}`); } app.get('/', (req, res) => { logs(req) res.send('/index') }) app.get('/register', (req, res) => { logs(req) res.send('/iregisterdex') }) app.get('/login', (req, res) => { logs(req) res.send('/login') }) app.listen(PORT, () => { console.log(`Server is running at http://localhost:${PORT}`) })
发现也并不完美
写个中间件
const express = require('express') // 加一个注释,用以说明,本项目代码可以任意定制更改 const app = express() const PORT = process.env.PORT || 3000 app.use((req, res, next) => { console.log(`${req.method}, ${req.url}, ${Date.now()}`); next() }) // // 挂载路由 // app.use('/api', router) // // 挂载统一处理服务端错误中间件 // app.use(errorHandler()) // 中间件写在要使用的逻辑前面 app.get('/', (req, res) => { res.send('/index') }) app.get('/register', (req, res) => { res.send('/register') }) app.get('/login', (req, res) => { res.send('/login') }) app.listen(PORT, () => { console.log(`Server is running at http://localhost:${PORT}`) })
中间件分类:
const express = require('express') const app = express() const PORT = process.env.PORT || 3000 // 以下3个都是应用程序级别中间件 app.use((req, res, next) => { console.log(`${req.method}, ${req.url}, ${Date.now()}`); next() }) app.get('/', (req, res) => { res.send('/index') }) app.get('/user', (req, res, next) => { console.log(req.method) next() }, function (req, res, next){ console.log('666') next() res.send('/user') }) app.listen(PORT, () => { console.log(`Server is running at http://localhost:${PORT}`) })
const express = require('express')
const router = express.Router()
router.get('/', (req, res) => {
console.log(req.methods)
res.send('/index')
})
router.get('/user', (req, res) => {
console.log(req.methods)
res.send('/user')
})
module.exports = router
const express = require('express')
const app = express()
const router = require('./router/index')
const routerVideo = require('./router/video')
const PORT = process.env.PORT || 3000
// 挂载路由
// app.use(router)
app.use('/api', router) // 添加前缀
// app.use('/video', routerVideo) // 添加前缀
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`)
})
// 错误处理中间件
app.use((err, req, res, next) => {
console.log(err)
res.status(500).send('service Error')
})
// app.use(express.urlencoded())
app.use(express.json())
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。