为什么在快递中使用此配置会收到Req.Req嵌套请求?

问题描述

编辑:如果嵌套请求或响应有问题。检查参数的顺序,或使用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
}