当我尝试引用 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
。您需要在路线开头指定 /
。
In Postman I set in headers
Content-Type: application/json
您正在发出 GET 请求。没有请求正文来描述类型。
请勿将 Content-Type
请求 header 与 Content-Type
响应 header 或 Accept
请求 header 混淆。
关于javascript - 如何在 Express 中创建路线并接收响应 (`Cannot GET/api/example' )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60934409/