考虑代码:

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


发送原始:

javascript - NODE JS &#43; Postman:无法修补URL-LMLPHP

我得到这个:

javascript - NODE JS &#43; Postman:无法修补URL-LMLPHP

为什么会这样呢?我哪里做错了 ?

最佳答案

您在路线中缺少斜线:

app.patch('/api/v1/tours/:id', (req, res) =>{

07-26 02:21