javascript - 如何在 express 中创建路由并接收响应(`Cannot GET/api/example')?
问题描述
当我尝试引用时,http://localhost: 8000/api/example
我在 Postman 中收到消息Cannot GET/api/example
(未找到 404)。我的目标是收到回复消息:
{
data: 'You hit example endpoint'
}
在邮递员我设置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
。您需要/
在路线的开头指定 。
在邮递员中,我设置了标题
Content-Type: application/json
您正在发出 GET 请求。没有描述类型的请求正文。
不要将Content-Type
请求标头与Content-Type
响应标头或Accept
请求标头混淆。
推荐阅读
- swift - 为什么我得到:“无法转换 '[String] 类型的值?' 到预期的参数类型“字符串?”
- jmeter - 从 jmeter 启动 aws cli 命令
- deep-learning - 来自 CNN 和 R-CNN 的区域特征
- javascript - 点击更改链接背景颜色
- python - Python Pandas 总数和日期
- ios - 如何锁定项目的 CocoaPods 版本?
- neo4j - 基于加权距离从源节点探索图
- android - react-native logcat 为什么一直显示:D/EGL_adreno: eglMakeCurrent?
- bash - Unix bash将带空格的字符串分配给变量
- java - 将字符串分配给 char 数组中的区域时,字符串的大小会增加