when call router.post method from url get error 404 not found in express.js

I have got a problem with Express, I am trying to use the app.post() function but it's not working and I don't know why...

Even though I have included the bodyParser()...

It returns 404 page not found error

var express = require('express');

var MongoClient = require('mongodb').MongoClient;

var router = express.Router();

router.post("/hello",(req, res) => {
        res.send('POST CALL');

/* GET home page. */
router.get('/', function(req, res, next) {
    MongoClient.connect('mongodb://localhost:27017/nicky', function (err, client) {
        if (err) throw err

        var db = client.db('nicky')

        db.collection('student').find().toArray(function (err, result) {
            if (err) throw err
module.exports = router;

GET is working properly, but POST is not.

I am not sure how rest of your code looks, but I have copied your provided snippet and it works this way:


const express = require('express');
const router = express.Router();

// curl -X POST http://localhost:3000/bar/hello
router.post("/hello",(req, res) => {
    res.send('It is POST');

// curl -X GET http://localhost:3000/bar/hi
router.get('/hi', function(req, res, next) {
    res.send('It is GET');

module.exports = router;


const express = require('express');
const bar = require('./express-post');
const app = express();

// curl -X GET http://localhost:3000/foo
app.get('/foo', function (req, res, next) {
    res.send('This is foo GET!');

// register
app.use('/bar', bar);


For complete running example clone node-cheat and run node express-post.

Post route not found! � Issue #2578 � expressjs/express � GitHub, I'm using express 4.12.2 and when i set (where router was created Compare plans � Contact Sales Router(); router.get('/user', function (req, res) { res.send(' here is the node test.js & [1] 7140 $ curl -i -XPOST When the form in index.jade is submitted, it is 404 not found. Now in the program im using express js not the natvie node js api. So hopefully some one who has ither used it or can figure it out would be able to help me out. But my problem is when i use app.post the server responds with a 404 can not get the request but if i use app.get or app.all it works just fine.

Maybe you haven't required and initialized body-parser! Just confirm once if you have included this:

  const bodyParser = require('body-parser');
  // support parsing of application/json type post data

  //support parsing of application/x-www-form-urlencoded post data
  app.use(bodyParser.urlencoded({ extended: true }));

Ajax POST with jQuery and Node.js Express always return 404: Not , Ajax POST with jQuery and Node.js Express always return 404: Not Found POST requests, but AJAX calls to this route always gives me the error 404: Not found. works fine: http://localhost:3001/integracoes/consultas router.get('/ consultas', requested by Ajax. router.post('/consulta/statuspedido', function(req, res) { var� Related. The dangers of trusting HTTP headers. HTTP headers can be set by users and they can be very dangerous if you are using their values anywhere in the application logic, or are writing or storing (and eventually writing) their values anywh

May be you forgot to use app.use('/',require('import route here')); in main app.

Guide, Express utilizes the HTTP verbs to provide a meaningful, expressive routing API. performs route lookup based on the current request url and HTTP method. function NotFound(msg){ this.name = 'NotFound'; Error.call(this, msg); Error. __ proto__ = Error.prototype; app.get('/404', function(req, res){ throw new NotFound; });� I am having same problem when using /wp-json/wp/v2/posts in my php script. when I call this URL directly from browser it works like a charm.

Express 5.x - API Reference, The function determines the file to serve by combining req.url with the When a file is not found, instead of sending a 404 response, it instead calls HTTP method routes (such as get , put , post , and so on) to router just like an application. In Express, 404 responses are not the result of an error, so the error-handler middleware will not capture them. This behavior is because a 404 response simply indicates the absence of additional work to do; in other words, Express has executed all middleware functions and routes, and found that none of them responded.

Node js route not working,always shows cannot post, when call router.post method from url get error 404 not found in express.js. 发布于 2020-05-03 05:20:50. I have got a problem with Express, I am trying to use the � An API is always needed to create mobile applications, single page applications, use AJAX calls and provide data to clients. An popular architectural style of how to structure and name these APIs and the endpoints is called REST(Representational Transfer State).

Express Tutorial Part 4: Routes and controllers, Express router post route not found, JWT authentication and Express working. This middleware is called as 'body How can I get Express.js to 404 only on missing routes?, error(function(err, req, res, next){ if (err (404) app.use(function (req, res, next) { return res.status(404).send({ message: 'Route'+req.url+' Not found. I included an "alert" to show the content of the "strURL" variable and it looks fine. When the AJAX call is made, the "error" part is triggered showing the 3 alerts: error, 404, Not Found. Also, I made sure my project runs in port 60642. The ïd" and "puntos" parameters of the function are not used for now.

  • Hi Nicky Vai, did you try to call from postman?
  • Can You provide snippet of your code error if have any. I think you should make a post request while accessing post router.
  • GET / 404 280.007 ms - 1112 GET /stylesheets/style.css 304 6.553 ms - - GET /hi 200 1.588 ms - 9 GET /hello 404 19.140 ms - 1112 GET /stylesheets/style.css 304 0.564 ms - not work
  • /hello is a POST not GET, so it is giving a valid error, 404 means not found. You can use postman or curl as I have provided instructions in code snippet above.