繁体   English   中英

如何在 express 中创建路由并接收响应(`Cannot GET/api/example')?

[英]How to create a route in express and receive a response (`Cannot GET/api/example')?

当我尝试引用http://localhost: 8000/api/example我在 Postman 中收到消息Cannot GET/api/example (Not Found 404)。 我的目标是收到回复消息:

{
   data: 'You hit example endpoint'
} 

在 Postman 中,我在headers Content-Type: application/json


const express = require('express');

const app = express();

app.get('api/example', (req, res) => {
    res.json({
        data: 'You hit data endpoint'
    });
});

const port = process.env.port || 8000;

app.listen(port, () => {
    console.log(`connect on the port ${port}`);
});
app.get('api/example', (req, res) => {

您请求的是/api/example而不是api/example 您需要在路由的开头指定/


在 Postman 中,我在标题Content-Type: application/json

您正在发出 GET 请求。 没有请求正文来描述类型。

不要将Content-Type请求标头与Content-Type响应标头或Accept请求标头混淆。

暂无
暂无

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

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