0

I am new in nodeJS, I have created this application:

const express = require('express');
const app = express();

app.use (express.json());
app.post('api/hostels', (req, res) => {
    const hostel = {
        id : hostels.length + 1,
        name: req.body.name
    };
    hostels.push(hostel);
    res.send(hostel);
});

I send this body in the PostMan raw body (json)

{
    "id": "4",
    "name" : "new Request"
}

but I am getting this error:

 <body>
        <pre>Cannot POST /api/requests</pre>
    </body>

3 Answers 3

2

Well, you did a small mistake while defining a route of the express. you have app.post('api/hostels', (req, res) => {}) instead you should have app.post('/api/hostels', (req, res) => {})

Sign up to request clarification or add additional context in comments.

Comments

0

There is a mistake in your post, theres a missing / in you app.post

it should be app.post('/api/hostels', (req, res) => { }

Comments

0

You are posting to /api/requests, your endpoint shows /api/hostels. Change the endpoint on your postman to /api/hostels.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.