考虑代码:
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
发送原始:
我明白了:
为什么会这样?我哪里做错了 ?
月关宝盒
函数式编程
相关分类