This is my code.
const express = require('express');
const app = express();
const bodyParser = require('body-parser')
const port = 9000;
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.post('mailServer', (req, res) =>{
res.send(req.body)
res.end();
})
app.listen(port, ()=>console.log(`Server listen on ${port}`)
@MirzadVa could you please also add the http request you're making? We'll need a reproducible example. When you say after sending post request, are you saying that your post is successful?
This works for me (notice you need the slash at the beginning of /mailServer:
const express = require('express');
const app = express();
const bodyParser = require('body-parser')
const port = 9000;
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.post('/mailServer', (req, res) =>{
res.send(req.body)
res.end();
})
app.listen(port, ()=>console.log(`Server listen on ${port}`))
@ryhinchey I'm prety much new at all of this. I'm trying to send post with postman. But even before I send it, it gives me this error. As soon as I type node server.js, in console I get 404
are you trying to go to https://localhost:9000/mailServer 404 in a browser? When I run this code, I don't get the error you receive. Can you create a reproducible repo I can look at that will demonstrate this issue happening?
I'm going to go ahead and close this issue as it's not something wrong with express. Feel free to keep commenting on the closed issue and we can hopefully be of help. Also - Stack Overflow and the express gitter are two great resources on your express journey :)
@ryhinchey Yes, that's right, that's the problem and I don't see why it's happening.
This is the repo. It's nothing, I'm just starting with express, but this is my big concern :D
https://github.com/MirzadVa/express-post
to be clear, are you trying to access localhost:9000/insert in the browser and it's not working or are you trying to make a POST request to localhost:9000/insert?
Actually, I'm trying to access it. I'm guessing if I can't access, I can't make post request either?
post requests are different from get requests. when you go to a webpage in the browser, the browser makes a get request for the site. you don't have a get method set up in your app.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
Add something like this to get your app to respond to get requests:
app.get('/mailServer', (req, res) =>{
res.send('ok')
})
If you're looking to learn more about express and web development with node, this is a solid resource:
https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs
Most helpful comment
post requests are different from get requests. when you go to a webpage in the browser, the browser makes a get request for the site. you don't have a get method set up in your app.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
Add something like this to get your app to respond to get requests:
If you're looking to learn more about express and web development with node, this is a solid resource:
https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs