express получение запроса любого типа
Чтобы получить запрос с body: <json>
, нужно использовать .use(express.json())
Для текста аналогично .use(express.text())
Для xml уже нужно подключать библиотеку express-xml-bodyparser
, .use(xmlparser())
Можно ли как-то без указаний .use(<>)
принимать любые типы боди?
createProxyServer() {
const logger = new ConsoleLoggerService('proxy-server');
const proxy = httpProxy.createProxyServer({ changeOrigin: true });
const app = express();
app.all('*', (req, res, next) => {
logger.log(`Got request to ${req.path}, method=${req.method} `);
next();
});
registerServiceController(app);
app.all('*', async (req, res, next) => {
const { path } = req;
const proxyPath = findProxyPath(path);
const proxyHost = proxyMap[proxyPath];
if (proxyHost) {
logger.log(`Path [${path}] will be proxying to [${proxyHost}] host`);
proxy.web(req, res, {
target: {
protocol: 'https:',
host: proxyHost,
port: 443,
},
});
} else {
logger.error(`Proxy mapping for [${path}] not found, request not proxied`);
}
next();
})
.use(express.json())
.use(express.text())
.use(express.raw({ type: 'application/pdf', limit: '10mb' }))
.use(xmlparser())
.all('*', async (req, res, next) => {
const { path } = req;
const proxyPath = findProxyPath(path);
const proxyHost = proxyMap[proxyPath];
await ProxyStorageService.saveRequest(req, proxyHost);
});
app.listen(PORT);
logger.log(`Server is listening on port ${PORT}`);
}
Источник: Stack Overflow на русском