Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 卷曲POST和PUT请求不起作用?_Node.js_Json_Curl - Fatal编程技术网

Node.js 卷曲POST和PUT请求不起作用?

Node.js 卷曲POST和PUT请求不起作用?,node.js,json,curl,Node.js,Json,Curl,**我正在学习JSON和新的后端开发 我已经通过npmstart命令运行了我的“server.js”文件。 当我运行curlpost和put命令时,它给出了一个错误。 我想不出我的代码有什么错误。 我用的是windows10 this is my server file named -"server.js" 请帮我找出错误** //名称-server.js const app = express() const bodyParser = require('body-parser') a

**我正在学习JSON和新的后端开发 我已经通过npmstart命令运行了我的“server.js”文件。 当我运行curlpost和put命令时,它给出了一个错误。 我想不出我的代码有什么错误。 我用的是windows10

    this is my server file named -"server.js"
请帮我找出错误**

//名称-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)
//服务器端的错误是

       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)
SyntaxError:JSON中位置0处的意外标记
在JSON.parse()处
在createStrictSyntaxError(C:\Users\login\Desktop\Learn\JSON\node\u modules\body parser\lib\types\JSON.js:158:10)
解析时(C:\Users\login\Desktop\Learn\JSON\node\u modules\body parser\lib\types\JSON.js:83:15)
在C:\Users\login\Desktop\Learn\JSON\node\u modules\body parser\lib\read.js:121:18
在invokeCallback(C:\Users\login\Desktop\Learn\JSON\node\u modules\raw body\index.js:224:16)
完成时(C:\Users\login\Desktop\Learn\JSON\node\u modules\raw body\index.js:213:7)
在IncomingMessage.onEnd(C:\Users\login\Desktop\Learn\JSON\node\u modules\raw body\index.js:273:7)
在IncomingMessage.emit(events.js:198:15)
在endReadableNT(_stream_readable.js:1142:12)
在处理和拒绝时(内部/process/task_queues.js:81:17)

POST/PUT请求上的
bodyParser.json()
很可能是发送格式错误的json的问题。请尝试:
curl-H“Content Type:application/json”-X POST-d“{\”first\u name\:\“Azat\”,“last\u name\:“khan\”}http://localhost:3000/profile
。我怀疑这可能是终端中的单引号问题。@Matt Harrison非常感谢,它很有效。您能告诉我如何解决终端中的单引号问题吗?最可能的问题是,在您的POST/PUT请求中将格式错误的json发送到
bodyParser.json()
,请尝试:
curl-H“内容类型:application/json”-X POST-d”{“first\u name\”:\“Azat\”,“last\u name\”:\“khan\”}”http://localhost:3000/profile
。我怀疑您的终端中的单引号可能有问题。@Matt Harrison非常感谢,它很有效。您能告诉我如何解决终端中的单引号问题吗
       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)