Home > OS >  Node.js / Intellij: http post request returns code 404
Node.js / Intellij: http post request returns code 404

Time:12-17

I get this response when testing the http method:

http://localhost:3000/rooms
HTTP/1.1 404 Not Found

This is the code for my routing / endpoint

router.post('/rooms', bodyparser.json(), (req, res) => {

let rooms;

try {
    rooms = {
        id:req.body.id,
        name: req.body.name
    };
    createRessource(filename)
    res.status(201).send(rooms);
} catch (error) {
    res.sendStatus(500)
}
refresh();

});

I tested with intellij editor this way:

POST http://localhost:3000/rooms
Content-Type: application/json 
{ "name": "Peter", "id": 4}

Is http://localhost:3000/rooms not the right endpoint? The get request for this endpoint is o.k

CodePudding user response:

Pretty sure IntelliJ is not at fault here as I've made many APIs on it (mostly express). If I were you the first thing I would check would be which port is my app listening on.

I assume you are using express (a few reasons such as usage of body-parser)

What port did you put in app.listen(port)? Maybe you put port 80?

Well considering you are actually receiving a 404 I'll cross that out.

Next thing would be the way you made your app use the router.

Remember, if you did something like

app.use(router, "/api")

and in router

router.post("/rooms", function)

then your final url to post would be /api/rooms and not just /rooms. So I suggest what you used on your app. If you don't want to put any preset path then just do

app.use(router)
  • Related