Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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
如何使用';体解析器';用JavaScript?_Javascript_Node.js_Express_Body Parser - Fatal编程技术网

如何使用';体解析器';用JavaScript?

如何使用';体解析器';用JavaScript?,javascript,node.js,express,body-parser,Javascript,Node.js,Express,Body Parser,我正在通过Node.js学习Web服务器。 当我尝试使用主体解析器时,我再也不能前进了。 状态代码成功。 这似乎是成功的。我收到了回复信息。但它不会显示在浏览器上 有什么问题? 我的代码如下 //basic-server.js const express = require('express') const cors = require('cors'); const app = express() const bodyParser = require('body-parser') const

我正在通过Node.js学习Web服务器。 当我尝试使用主体解析器时,我再也不能前进了。 状态代码成功。 这似乎是成功的。我收到了回复信息。但它不会显示在浏览器上

有什么问题? 我的代码如下

//basic-server.js

const express = require('express')
const cors = require('cors');
const app = express()

const bodyParser = require('body-parser')
const jsonParser = bodyParser.json()

const PORT = 5000;
const ip = 'localhost';

app.use(cors())

app.get('/', (req, res) =>{
  res.send("hello")
})

app.post('/lower', jsonParser, (req, res) =>{
  
 res.send(req.body.body.toLowerCase())
  
})
app.post('/upper', jsonParser, (req, res) =>{
  
  res.send(req.body.body.toUpperCase())
})



app.listen(PORT, ip, () => {
  console.log(`http server listen on ${ip}:${PORT}`);
});
开发人员工具控制台上的错误消息

'Uncaught (in promise) SyntaxError: Unexpected token A in JSON at position 0
Promise.then (async)
post @ App.js:21
toUpperCase @ App.js:31'.
以下信息来自“网络”选项卡

-preflight
Request URL: http://localhost:5000/upper
Request Method: OPTIONS
Status Code: 204 No Content
Remote Address: 127.0.0.1:5000
Referrer Policy: strict-origin-when-cross-origin
Access-Control-Allow-Headers: content-type
Access-Control-Allow-Methods: GET,HEAD,PUT,PATCH,POST,DELETE
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 0
Date: Fri, 28 May 2021 10:15:31 GMT
Keep-Alive: timeout=5
Vary: Access-Control-Request-Headers
X-Powered-By: Express
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Cache-Control: no-cache
Connection: keep-alive
Host: localhost:5000
Origin: null
Pragma: no-cache
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: cross-site
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36

您正在正确使用
bodyparser
。问题在于您的代码中,您只返回了一个符号
a
,而客户端试图解析这个不正确的JSON,这就是它失败的原因


您可以将
res.send(req.body.body.toUpperCase())
更改为
res.send(JSON.stringify(req.body.body.toUpperCase())
,以解决此问题。

您得到的响应不是JSON格式,因此请尝试将其添加到server.js中请求上方的代码中

app.use(bodyparser.urlencoded({extended:false}));

use(bodyparser.json())如何调用
post
-preflight
Request URL: http://localhost:5000/upper
Request Method: OPTIONS
Status Code: 204 No Content
Remote Address: 127.0.0.1:5000
Referrer Policy: strict-origin-when-cross-origin
Access-Control-Allow-Headers: content-type
Access-Control-Allow-Methods: GET,HEAD,PUT,PATCH,POST,DELETE
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 0
Date: Fri, 28 May 2021 10:15:31 GMT
Keep-Alive: timeout=5
Vary: Access-Control-Request-Headers
X-Powered-By: Express
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Cache-Control: no-cache
Connection: keep-alive
Host: localhost:5000
Origin: null
Pragma: no-cache
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: cross-site
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36
-fetch
Request URL: http://localhost:5000/upper
Request Method: POST
Status Code: 200 OK
Remote Address: 127.0.0.1:5000
Referrer Policy: strict-origin-when-cross-origin
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 1
Content-Type: text/html; charset=utf-8
Date: Fri, 28 May 2021 10:15:31 GMT
ETag: W/"1-bc1M4j2I4u6VaLpUbAB8Y9kTHBs"
Keep-Alive: timeout=5
X-Powered-By: Express
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 12
Content-Type: application/json
Host: localhost:5000
Origin: null
Pragma: no-cache
sec-ch-ua: "Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"
sec-ch-ua-mobile: ?0
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: cross-site
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36
{body: "a"}
body: "a"
-Response
A