NODE JS +邮递员:无法修补URL

时间:2019-08-31 09:44:21

标签: javascript node.js express postman

考虑代码:

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

发送原始数据:

enter image description here

我明白了:

enter image description here

为什么会发生?我哪里做错了 ?

2 个答案:

答案 0 :(得分:3)

您在路线中缺少斜线:

sourceMap

答案 1 :(得分:2)

使用定义路线时,您会错过此处的前导/(斜杠)

app.patch('/...', (req, res) => { 
    ... 
    ...
 });

Express在定义路线时需要前导斜线:)希望这会有所帮助