尝试 POST 请求时节点 js 错误

Node js error when trying the POST request

我不明白为什么会出现 "cannot post" 错误。请帮忙

const app = express();

//Adding Body Parser middleware
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());

app.get('/',(req,res)=>{
    res.json({
        status: "200"
    })
})

app.post('/',(req,res)=>{
    var city = req.body.city;
    res.status(200).json({stat: city})
})

您是否使用 app.listen(port) 启动应用程序(port 是您的端口)。

我使用了你的代码(添加app.listen(port))并且post没问题。

这是我的代码:

const bodyParser = require('body-parser')
const express = require('express')
const app = express();
//Adding Body Parser middleware
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());

app.get('/',(req,res)=>{
    res.json({
        status: "200"
    })
})

app.post('/',(req,res)=>{
    var city = req.body.city || '1';
    res.status(200).json({stat: city})
})

app.listen(4567)

我使用 Insomnia,这是我的结果:

可能是端口的问题。更改端口号可能会有所帮助。