首页 > 解决方案 > 无法在 Postman 中为简单的 Node.js 应用程序获得响应

问题描述

我正在尝试运行下面显示的代码

const express = require('express');
const {randomBytes}=require('crypto');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json);
const posts = {};
app.get('/posts', (req,res)=> {
  res.send(posts);
});
app.post('/posts',(req,res) => {
    console.log('I am in')
    const id = randomBytes(4).toString('hex');
    const title = req.body('title');
    const postContent = req.body('content');
    posts[id]={
      id,title,postContent
    };
    res.status(201).send(posts[id])
});
app.listen(4000,() => {
  console.log('Listening on 4000')
});

当我尝试在 Postman 中获得回复时,我没有得到回复。相反,“发送请求”消息会永远显示

在此处输入图像描述

GET 和 POST 请求都给出相同的错误。

标签: node.jsexpresspostman

解决方案


推荐阅读