CURL POST和PUT请求不起作用?

时间:2019-06-06 11:21:37

标签: node.js json curl

**我正在学习JSON和后端开发的新知识        我已经通过npm start命令运行了“ server.js”文件。         当我运行curl发布并放置命令时,它给出了错误。        我无法弄清楚代码中的错误是什么。         我正在使用Windows 10 CMD

    this is my server file named -"server.js"

请帮助我确定错误**

// NAME -server.js

const app = express()
const bodyParser = require('body-parser')
app.use(bodyParser.json())
let profile = {
    username: 'azat',
    email: '[reducted]',
    url: 'http://azat.co'
}
app.get('/profile', (req, res) => {
    res.send(profile)
})
app.post('/profile', (req, res) => {
    profile = req.body
    console.log('created', profile)
    res.sendStatus(201)
})
app.put('/profile', (req, res) => {
    Object.assign(profile, req.body)
    console.log('updated', profile)
    res.sendStatus(204)
})
app.delete('/profile', (req, res) => {
    profile = {}
    console.log('deleted', profile)
    res.sendStatus(204)
})

app.listen(3000)
   // My CURL command


    curl -H "Content-Type:application/json" -X POST -d '{"first_name":"Azat","last_name":"khan"}'  "http://localhost:3000/profile"

//服务器端错误是

       SyntaxError: Unexpected token ' in JSON at position 0
    at JSON.parse (<anonymous>)
    at createStrictSyntaxError (C:\Users\login\Desktop\Learn\JSON\node_modules\body-parser\lib\types\json.js:158:10)
    at parse (C:\Users\login\Desktop\Learn\JSON\node_modules\body-parser\lib\types\json.js:83:15)
    at C:\Users\login\Desktop\Learn\JSON\node_modules\body-parser\lib\read.js:121:18
    at invokeCallback (C:\Users\login\Desktop\Learn\JSON\node_modules\raw-body\index.js:224:16)
    at done (C:\Users\login\Desktop\Learn\JSON\node_modules\raw-body\index.js:213:7)
    at IncomingMessage.onEnd (C:\Users\login\Desktop\Learn\JSON\node_modules\raw-body\index.js:273:7)
    at IncomingMessage.emit (events.js:198:15)
    at endReadableNT (_stream_readable.js:1142:12)
    at processTicksAndRejections (internal/process/task_queues.js:81:17)

0 个答案:

没有答案