使用JSON进行POST请求时出错

这是我的server.js文件代码。我试图在用户对象中推送JSON内容,但出现以下错误。请告诉我我要去哪里错了。

const express = require('express')
const app = express()
const bcrypt = require('bcrypt')
const bodyParser = require('body-parser')
app.use(express.json())
const users = []

app.get('/users', (req, res) => {
  JSON.stringify(users)
  res.json(users)
})

app.post('/users', (req, res) => {
  const user = {
    name: req.body.name,
    password: req.body.password
  }
  users.push(user)
  res.status(201).send()
})

app.listen(3000, console.log("server started"));

我在VS代码中使用了一个称为REST client的扩展。 当我触发POST请求时,显示错误

GET http: //localhost:3000/users

#####

POST http: //localhost:3000/users
Content-Type: application/json

{
  "name": "Tanay",
  "password": "password"
}

SyntaxError:JSON输入意外结束

at JSON.parse (<anonymous>)
at parse (C:\Users\TANAY RAJ\Desktop\nodePassport\Wsimplified\node_modules\body-parser\lib\types\json.js:89:19)
at C:\Users\TANAY RAJ\Desktop\nodePassport\Wsimplified\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (C:\Users\TANAY RAJ\Desktop\nodePassport\Wsimplified\node_modules\raw-body\index.js:224:16)
at done (C:\Users\TANAY RAJ\Desktop\nodePassport\Wsimplified\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (C:\Users\TANAY RAJ\Desktop\nodePassport\Wsimplified\node_modules\raw-body\index.js:273:7)
at IncomingMessage.emit (events.js:322:22)
at endReadableNT (_stream_readable.js:1187:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
评论