首页 > 解决方案 > 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

发送原始:

在此处输入图像描述

我明白了:

在此处输入图像描述

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

标签: javascriptnode.jsexpresspostman

解决方案


您在路线中缺少斜线:

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

推荐阅读