我对诺德很陌生.js/express/postman所以如果这个问题是新手,请原谅.我有以下代码是根据在线教程生成的.我用postman 来测试.问题是,当我添加第app.use(express.json)行时,postman 无法发送post请求.它被困在"发送请求…".如果我排除了这一行,那么"Get"请求将起作用.谁知道为什么...基于这个代码.这就是全部:

谢谢

const Joi = require('joi'); // Put required modules at the top
const express = require('express');
const app = express();

app.use(express.json);

const courses = [
    { id: 1, name: 'course1' },
    { id: 2, name: 'course2' },
    { id: 3, name: 'course3' },
];

app.get('/', (req, res) => {
    res.send('Hello world!!!');
});

app.get('/api/courses', (req, res) => {
    res.send(courses);
});

app.post('/api/courses', (req, res) => {    
    const { error } = validateCourse(req.body);
    if (error) return res.status(400).send(results.error.details[0].message);

    const course = {
        id: courses.length + 1,
        name: req.body.name
    };

    courses.push(course);
    res.send(course);   // assigning the id on the server. return course to client
});

app.get('/api/courses/:id', (req, res) => {
    const course = courses.find(c => c.id === parseInt(req.params.id))
    if (!course) return res.status(404).send('The course with the given ID was not found');

    res.send(course);
});

app.put('/api/courses/:id', (req, res) => {
    const course = courses.find(c => c.id === parseInt(req.params.id))
    if (!course) return res.status(404).send('The course with the given ID was not found');
    
    const { error } = validateCourse(req.body);
    if (error) return res.status(400).send(results.error.details[0].message);

    course.name = req.body.name;
    res.send(course);
});

app.delete('/api/courses/:id', (req, res) => {
    const course = courses.find(c => c.id === parseInt(req.params.id))
    if (!course) return res.status(404).send('The course with the given ID was not found');
    
    const index = courses.indexOf(course);
    courses.splice(index, 1);

    res.send(course);
})

function validateCourse(course) {
    const schema = {
        name: Joi.string().min(3).required()
    };

    return Joi.ValidationError(course, schema);   
}

// PORT
const port = 3000;
app.listen(port, () => console.log(`listening on port ${port}...`));

推荐答案

问题是json中间件(app.use(express.json);),它要求您返回有效的json(Content-Type: application/json).

我将您的示例更新为以下内容,使其运行:

app.get('/', (req, res) => {
    res.json({msg: 'Hello world!!!'});
});

这是一个有效的版本:https://pastebin.com/06H6LCD2

Node.js相关问答推荐

如何使用MongoDB在Node.js 中向数组中添加项?

将 POST 的 json 变量格式化为 lambda

GitLab 依赖扫描需要源代码中的 package-lock.json 才能执行

SvelteKit应用程序立即退出,没有错误

如何修复我的 NodeJS SSE 写入函数以在后续调用中更新 HTML?

node_modules/preact/src/jsx.d.ts:2145:22 - 错误 TS2304:找不到名称SVGSetElement

Google Calendar API FreeBusy 外部用户

NodeJS 后端发布请求将数据作为NULL值发布到 SQL Server 表

仅显示用户在 Reactjs 中使用服务器端发布的帖子是 Node Js、Mongodb

GridFS 大文件下载使 Node.js 服务器崩溃. MongoServerError:排序超出了字节的内存限制

即使部署成功,也不会触发 Firebase 函数来填充 Firestore 集合.为什么?

我可以通过 JNI 从 Node.js 调用 Java 吗?如何?

在Go中,编写非阻塞代码有意义吗?

在 Node.js 中的两个不同进程之间进行通信

通过 POST 请求将数据从 node.js 服务器发送到 node.js 服务器

当我try 向我的 S3 存储桶 (Node.js) 发送内容时,AWS 缺少凭证

将 Heroku App 连接到 Atlas MongoDB 云服务

使用 Monit 而不是基本的 Upstart 设置有什么好处?

Express js 阻止 GET /favicon.ico

Google Electron 表格 api 请求的身份验证范围不足