I have built a simple NodeJS app to demo REST API request handling. Then I used Nginx to proxy my Node app. Then for testing, I used Postman to do a GET request which returns:
"message": "Handling GET request to /products"
which is good. But changing in Postman from GET to POST and then sending the request, it returns the same answer.
"message": "Handling GET request to /products"
If I use curl locally to make the requests (both GET and POST) I recieve good responses.
I use PM2 and running server.js
server.js:
const http = require('http');
const app = require('./app');
const port = 3000;
const server = http.createServer(app);
server.listen(port);
app.js:
const express = require('express');
const app = express();
const productRoutes = require('./api/routes/products');
app.use('/products', productRoutes);
module.exports = app;
products.js:
const express = require('express');
const router = express.Router();
router.get('/', (req, res, next) => {
res.status(200).json({
message: 'Handling GET request to /products'
});
});
router.post('/', (req, res, next) => {
res.status(200).json({
message: 'Handling POST request to /products'
});
});
module.exports = router;
ngix site config
location / {
try_files $uri $uri/ =404;
}
location /api {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /api/products {
proxy_pass http://localhost:3000/products;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
after I do the Postman POST request, here is the access.log from Nginx. The GET request was not called. It is just POST, though there is shown a GET request.
“POST /api/products HTTP/1.1” 301 178 “-” “PostmanRuntime/7.6.0” “GET /api/products HTTP/1.1” 200 47 “http://flipit.ro/api/products” “PostmanRuntime/7.6.0” The “-” shows me that POST request is not forwarded where it should.
I am new to Nginx/NodeJS so this may have a very simple and obvious solution, but don’t judge me, as I am used to Apache/PHP. Also I am pretty bad at explaining things, but I hope you understand my problem.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Am having this problem also.when i use postman or curl i get normal post requests but when i intergrate it to a payment api from another domain the PPST requests are actually coming as 404 GET requests
Try this:
location /api {
proxy_pass http://localhost:3000/api;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /api/products {
proxy_pass http://localhost:3000/api/products;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
it’s a good practice to match location with backend URI.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.