javascript - NODE JS + Postman:无法修补 URL
问题描述
考虑代码:
const fs = require('fs');
const express = require('express');
const app = express();
const bodyParser = require('body-parser')
// use middleware
app.use(express.json());
app.use(bodyParser.json());
const fileLocation = `${__dirname}/dev-data/data/tours-simple.json`;
const theTours = JSON.parse(fs.readFileSync(fileLocation));
app.patch('api/v1/tours/:id', (req, res) =>{
if (req.params.id * 1 > theTours.length) {
return res.status(404).json({
status: 'fail',
message: 'Invalid ID'
});
}
res.status(200).json({
status: 'success',
data: {
tour: '<Updated tour here ...>'
}
});
});
const port = 3000;
app.listen(port, () => {
console.log(`App is running on port ${port}`);
});
当我尝试从 Postman 修补 URL 时:
Action PATCH
URL : 127.0.0.1:3000/api/v1/tours/3
发送原始:
我明白了:
为什么会这样?我哪里做错了 ?
解决方案
您在路线中缺少斜线:
app.patch('/api/v1/tours/:id', (req, res) =>{
推荐阅读
- javascript - 在 React Native 中使用多个复选框处理状态
- powerapps - 尝试切换元素的可见性时如何在 PowerApps 中正确使用 If 语句?
- java - Java8 UnitTesting CompletableFuture 异常
- angular - 将页面状态作为参数保存到当前路由
- python - 尝试将多处理应用于将 xml 处理为 csv 的函数
- alloy - 合金翻译限制:关系的复杂性
- javascript - 放置在 JavaScript 文件中名为 HTML 的变量中添加的标签中时,onClick 不起作用
- vue.js - Vue输入中的特殊字符问题
- spring - 如何在 Legecy(Spring web-mvc)代码中发现我的服务(Spring Boot 应用程序)
- python - 我可以用布尔变量绘制时间序列吗?