node.js - 无法在 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 请求都给出相同的错误。
解决方案
推荐阅读
- java - 带有类对象的哈希图
- php - 如何正确计算 php 日期时间的剩余时间
- python - OBS Studio python 热键
- react-native - React Native TextInput 在 android 上显示未定义
- sql - 是否可以为基于字符串/uuid 的主键创建索引,以便能够通过相似性快速搜索(例如嘈杂的 uuid)?
- java - 在 Java 中的方法之前崩溃
- python - Python 正则表达式分组查找器
- python - 枕头安装 PyPy3 缺少 zlib
- git - 如何完全删除现有的 git 用户?
- react-native - 同一屏幕上的选项卡和堆栈导航器