Click here to Skip to main content
15,887,275 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is my gateway.js file

JavaScript
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
JavaScript
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
JavaScript
//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

JavaScript
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
JavaScript
"dependencies": {
    "body-parser": "^1.20.2",
    "express": "^4.18.2",
    "http-proxy-middleware": "^2.0.6"
  }
Posted
Updated 6-Aug-23 22:24pm
v5
Comments
Chris Copeland 7-Aug-23 5:05am    
Not sure if this is relevant or not but all the documentation I saw for express endpoints suggested using app.get('/path', (req, res) => {}) and app.post('/path', (req, res) => {}) to define the endpoints?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900