简体   繁体   English

node js express 无法发布 - 获取无法发布错误消息

[英]node js express cannot post - getting Cannot POST error message

I am new in nodeJS, I have created this application:我是 nodeJS 的新手,我创建了这个应用程序:

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)我在 PostMan 原始正文 (json) 中发送此正文

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

but I am getting this error:但我收到此错误:

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

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) => {})你有app.post('api/hostels', (req, res) => {})而你应该有app.post('/api/hostels', (req, res) => {})

You are posting to /api/requests , your endpoint shows /api/hostels .您正在发布到/api/requests ,您的端点显示/api/hostels Change the endpoint on your postman to /api/hostels .将邮递员的端点更改为/api/hostels

There is a mistake in your post, theres a missing / in you app.post您的帖子中有错误,您的 app.post 中缺少/

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM