使用 json-server ,我们可以声明 CLI 中间件
You can add your middlewares from the CLI using --middlewares option:
// hello.js
module.exports = (req, res, next) => {
res.header('X-Hello', 'World')
next()
}
json-server db.json --middlewares ./hello.js
json-server db.json --middlewares ./first.js ./second.js
但是如何在我的 node.js 代码中声明使用 json-server 中间件?
我试图将它推入中间件数组
const jsonServer = require('json-server')
const apiServer = jsonServer.create() // Returns an Express server for the API
const apiMiddlewares = jsonServer.defaults() // Returns middlewares used by JSON Server.
apiMiddlewares.push((req, res, next) => {
console.log(new Date(), req.method, req.url)
next()
})
console.log('API MIDDLEWARES ARRAY: ', apiMiddlewares)
它已添加到数组中,但从未使用过...没有显示