json-server 没有使用中间件文件中提供的代码
json-server does not use the code provided in the middleware file
我正在使用 json-server 来模拟一些 api 调用响应以进行 front-end 开发结果证明添加中间件在我的情况下并没有真正起作用:
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "concurrently -k \"json-server --watch api-faked/db.json --routes api-faked/routes.json --middlewares api-faked/middleware.js --ro\" \"vue-cli-service serve\"",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
开始时:
[1] INFO Starting development server...
[0]
[0] \{^_^}/ hi!
[0]
[0] Loading api-faked/db.json
[0] Loading api-faked/routes.json
[0] Loading api-faked/middleware.js
[0] Done
[0]
似乎 middleware.js
被 json-server 考虑了,但是,一个简单的例子,例如下面的 middleware.js
:
module.exports = (req, res, next) => {
res.header('X-Hello', 'World')
next()
}
没有在我的回复中添加任何 header headers:
Request URL: http://localhost:3000/api/v1/operations/?code_like=adi
Request Method: OPTIONS
Status Code: 204 No Content
Remote Address: 127.0.0.1:3000
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: authorization
Access-Control-Allow-Methods: GET,HEAD,PUT,PATCH,POST,DELETE
Access-Control-Allow-Origin: http://localhost:8080
Connection: keep-alive
Content-Length: 0
Date: Thu, 18 Jul 2019 12:23:53 GMT
Vary: Origin, Access-Control-Request-Headers
X-Powered-By: Express
我在这里错过了什么?
Side-note:实际上从 middleware.js
中删除 next
甚至不会改变任何东西,就像即使已将中间件添加到 json-server 配置中一样似乎在任何时间点都不是 运行。
您可能错过了对 tie/link Express 应用中间件的实际调用。尝试调用:
app.use(enter_middleware_function_here);
这是假设您实例化您的应用类似于:
const express = require('express')
const app = express()
我的解决方案是使用 json-server 的模块样式,它基本上涉及创建一个 js
文件和重新创建路由器,否则路由器会使用 db.json
自动创建routes.json
,与自动模式不同,它支持添加一个可以拦截几乎所有内容的中间件。
例子
const fs = require('fs');
const jsonServer = require('json-server');
const path = require('path');
const server = jsonServer.create();
const defaultMiddleware = jsonServer.defaults();
// It is recommended to use the bodyParser middleware before any other middleware in your application
server.use(jsonServer.bodyParser);
server.use(defaultMiddleware);
// Define custom routes (routes.json)
const routes = JSON.parse(fs.readFileSync(path.join(__dirname, 'routes.json')));
server.use(jsonServer.rewriter(routes));
// Add custom middleware before JSON Server router
const customMiddleware = require(path.join(__dirname, 'middleware.js'));
server.use(customMiddleware);
// This is where `json-server`'s magic happens ;)
const router = jsonServer.router(path.join(__dirname, 'db.json'));
// Start the application by listening to port 3000,
// Although this won't print the nice starting message you see when
// running `json-server` as CLI command, it still runs the app correctly.
server.use(router);
server.listen(3000, () => {
console.log('JSON Server is running')
});
文件夹api-faked
的结构为:
server.js
middleware.js
db.json
routes.json
而不是像那样提供应用程序(在 package.json
中):
"serve": "concurrently -k \"json-server --watch api-faked/db.json --routes api-faked/routes.json --middlewares api-faked/middleware.js --ro\" \"vue-cli-service serve\"",
现在是这样的:
"serve": "concurrently -k \"nodemon api-faked/server.js --watch api-faked/*.*\"
我还使用 nodemon,如果任何给定文件发生任何更改(即 --watch api-faked/*.*
)
,它会重新启动 json-服务器
多亏了GitHub issue: json-server does not use the code provided in the middleware file
中给出的答案,我才走上正轨
我正在使用 json-server 来模拟一些 api 调用响应以进行 front-end 开发结果证明添加中间件在我的情况下并没有真正起作用:
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "concurrently -k \"json-server --watch api-faked/db.json --routes api-faked/routes.json --middlewares api-faked/middleware.js --ro\" \"vue-cli-service serve\"",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
开始时:
[1] INFO Starting development server...
[0]
[0] \{^_^}/ hi!
[0]
[0] Loading api-faked/db.json
[0] Loading api-faked/routes.json
[0] Loading api-faked/middleware.js
[0] Done
[0]
似乎 middleware.js
被 json-server 考虑了,但是,一个简单的例子,例如下面的 middleware.js
:
module.exports = (req, res, next) => {
res.header('X-Hello', 'World')
next()
}
没有在我的回复中添加任何 header headers:
Request URL: http://localhost:3000/api/v1/operations/?code_like=adi
Request Method: OPTIONS
Status Code: 204 No Content
Remote Address: 127.0.0.1:3000
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: authorization
Access-Control-Allow-Methods: GET,HEAD,PUT,PATCH,POST,DELETE
Access-Control-Allow-Origin: http://localhost:8080
Connection: keep-alive
Content-Length: 0
Date: Thu, 18 Jul 2019 12:23:53 GMT
Vary: Origin, Access-Control-Request-Headers
X-Powered-By: Express
我在这里错过了什么?
Side-note:实际上从 middleware.js
中删除 next
甚至不会改变任何东西,就像即使已将中间件添加到 json-server 配置中一样似乎在任何时间点都不是 运行。
您可能错过了对 tie/link Express 应用中间件的实际调用。尝试调用:
app.use(enter_middleware_function_here);
这是假设您实例化您的应用类似于:
const express = require('express')
const app = express()
我的解决方案是使用 json-server 的模块样式,它基本上涉及创建一个 js
文件和重新创建路由器,否则路由器会使用 db.json
自动创建routes.json
,与自动模式不同,它支持添加一个可以拦截几乎所有内容的中间件。
例子
const fs = require('fs');
const jsonServer = require('json-server');
const path = require('path');
const server = jsonServer.create();
const defaultMiddleware = jsonServer.defaults();
// It is recommended to use the bodyParser middleware before any other middleware in your application
server.use(jsonServer.bodyParser);
server.use(defaultMiddleware);
// Define custom routes (routes.json)
const routes = JSON.parse(fs.readFileSync(path.join(__dirname, 'routes.json')));
server.use(jsonServer.rewriter(routes));
// Add custom middleware before JSON Server router
const customMiddleware = require(path.join(__dirname, 'middleware.js'));
server.use(customMiddleware);
// This is where `json-server`'s magic happens ;)
const router = jsonServer.router(path.join(__dirname, 'db.json'));
// Start the application by listening to port 3000,
// Although this won't print the nice starting message you see when
// running `json-server` as CLI command, it still runs the app correctly.
server.use(router);
server.listen(3000, () => {
console.log('JSON Server is running')
});
文件夹api-faked
的结构为:
server.js
middleware.js
db.json
routes.json
而不是像那样提供应用程序(在 package.json
中):
"serve": "concurrently -k \"json-server --watch api-faked/db.json --routes api-faked/routes.json --middlewares api-faked/middleware.js --ro\" \"vue-cli-service serve\"",
现在是这样的:
"serve": "concurrently -k \"nodemon api-faked/server.js --watch api-faked/*.*\"
我还使用 nodemon,如果任何给定文件发生任何更改(即 --watch api-faked/*.*
)
多亏了GitHub issue: json-server does not use the code provided in the middleware file
中给出的答案,我才走上正轨