[英]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.