为什么我在 express 中使用此配置得到 Req.Req(嵌套请求)?
Why I'm getting Req.Req (Nested request) with this configuration in express?
编辑:如果您遇到嵌套请求或响应的问题。检查参数的顺序,或使用 express.router({margeParams: true}) 选项。
我的请求正在被另一个对象请求包装。
我必须通过这种方式访问 'req.body' -> req.req.body
那是因为我的配置?
我有一个具有此配置的快速服务器:
import express from 'express';
import routes from './routes.js';
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use('/api', routes);
server.listen(80);
这是routes.js
import express from 'express';
import webhooksRoutes from './controllers/Webhooks/webhooksRoutes.js';
const routes = express.Router();
routes.use('/webhooks', webhooksRoutes);
这是webhooksRoutes.js
import express from 'express';
const userRoutes = express.Router();
userRoutes.post('/orders', orderWebhooks);
这是orderWebhooks.js
const wirecardOrdersWebhooks = (res, req) => {
// here the "real" request that come to the server is in res.res
const realReq = req.req
}
这里第一个参数应该是res之前的req;
const wirecardOrdersWebhooks = (req, res) => {
// here the "real" request that come to the server is in res.res
const realReq = req.body
}
编辑:如果您遇到嵌套请求或响应的问题。检查参数的顺序,或使用 express.router({margeParams: true}) 选项。
我的请求正在被另一个对象请求包装。
我必须通过这种方式访问 'req.body' -> req.req.body 那是因为我的配置?
我有一个具有此配置的快速服务器:
import express from 'express';
import routes from './routes.js';
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use('/api', routes);
server.listen(80);
这是routes.js
import express from 'express';
import webhooksRoutes from './controllers/Webhooks/webhooksRoutes.js';
const routes = express.Router();
routes.use('/webhooks', webhooksRoutes);
这是webhooksRoutes.js
import express from 'express';
const userRoutes = express.Router();
userRoutes.post('/orders', orderWebhooks);
这是orderWebhooks.js
const wirecardOrdersWebhooks = (res, req) => {
// here the "real" request that come to the server is in res.res
const realReq = req.req
}
这里第一个参数应该是res之前的req;
const wirecardOrdersWebhooks = (req, res) => {
// here the "real" request that come to the server is in res.res
const realReq = req.body
}