Below is my gateway.js file
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
const PORT = 9999;
app.use(bodyParser.json());
const { createProxyMiddleware } = require('http-proxy-middleware');
app.use('/flight', createProxyMiddleware({
target: 'http://localhost:9998',
changeOrigin: true,
preserveBody: true,
timeout: 10000
}));
const startServer = async () => {
app.listen(PORT, () => {
console.log(`gateway server is running on port ${PORT}`);
});
};
startServer();
I am making call from post man to this above file which i am running on port 9999 and this should redirect to this below code
This below is code of index.js file
const express = require('express');
const bodyParser = require('body-parser');
const PORT = 9998;
const app = express();
const Cors = require('cors');
app.use(Cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/flight', async (req, res) => {
return res.status(200).json({
data: req.body,
message: "reached the flight index"
});
});
const setupAndStartServer = async () => {
app.listen(PORT, async () => {
console.log(`flight server started at port number : ${PORT}`);
});
};
setupAndStartServer();
when I am sending something in x www form urlencoded the request is not recieved but when I am not sending anything in the request body then I am getting the desired output
{
"data": {},
"message": "reached the flight index"
}
In x-www-form-urlencoded i am only sending key ->name ,value ->someName
why when sending the body the above code is not working
I have made a get/post/patch request through postman on -> localhost:9999/flight
also when body is sent in request through postman it continues to show that sending request and after few minutes this below error got printed on the index.js console
flight server started at port number : 9998
BadRequestError: request aborted
at IncomingMessage.onAborted (C:\Users\nikhi\Desktop\back\test\node_modules\raw-body\index.js:245:10)
at IncomingMessage.emit (node:events:513:28)
at IncomingMessage._destroy (node:_http_incoming:224:10)
at _destroy (node:internal/streams/destroy:109:10)
at IncomingMessage.destroy (node:internal/streams/destroy:71:5)
at abortIncoming (node:_http_server:754:9)
at socketOnClose (node:_http_server:748:3)
at Socket.emit (node:events:525:35)
at TCP.<anonymous> (node:net:322:12)
What I have tried:
Below is what i have installed
"dependencies": {
"body-parser": "^1.20.2",
"express": "^4.18.2",
"http-proxy-middleware": "^2.0.6"
}