Node.js 表示意外标记k

Node.js 表示意外标记k,node.js,express,body-parser,Node.js,Express,Body Parser,我有一个express应用程序,它被设计成一个简单的restful api。 这是我的app.js文件,添加了主体解析器中间件 app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: true})); 使用bodyparser 1.15.2 这是我的基本路由文件 ... r.post('/search/:keyword', function(req, res){ var __keyword = req.para

我有一个express应用程序,它被设计成一个简单的restful api。 这是我的app.js文件,添加了主体解析器中间件

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
使用bodyparser 1.15.2

这是我的基本路由文件

... 
r.post('/search/:keyword', function(req, res){
var __keyword = req.params.keyword,
console.log(req.body);
我使用postman发送请求,当我使用raw选项卡发送带有application/json头的请求时,结果很好。这种发送请求的方式生成的代码如下所示

POST /api/search/mykeyword HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache
{
    "key": "value"
}
POST /api/search/mykeyword HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache

{
    "key": "value"
}
在postman中使用x-form-urlencoded选项时,我遇到了这个错误

SyntaxError: Unexpected token k
    at parse (D:<projectpath>\node_modules\body-parser\lib\types\json.js:83:15)
    at D:<projectpath>\node_modules\body-parser\lib\read.js:116:18
    at invokeCallback (D:<projectpath>\node_modules\body-parser\node_modules\raw-body\index.js:262:16)
    at done (D:<projectpath>\node_modules\body-parser\node_modules\raw-body\index.js:251:7)
    at IncomingMessage.onEnd (D:<projectpath>\node_modules\body-parser\node_modules\raw-body\index.js:307:7)
    at emitNone (events.js:67:13)
    at IncomingMessage.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:913:12)
    at nextTickCallbackWith2Args (node.js:442:9)
    at process._tickCallback (node.js:356:17)
对我来说,这看起来完全一样,但它产生了这个错误?这是bodyparser、express还是postman中的错误?还是这是我的错? 只打电话的时候

app.use(bodyParser.urlencoded({extended: true}));

req.body对象始终为空,无论使用原始body时使用哪种方法,请选择
JSON(application/JSON)
而不是
Text
,然后将填充
req.body

您还可以删除app.use(bodyParser.urlencoded({extended:true}))

如果要使用
application/x-www-form-urlencoded
,请检查邮递员是否在标题选项卡中没有
application/json

否则bodyParser将抛出此错误


使用原始正文时,选择
JSON(application/JSON)
而不是
Text
,将填充
req.Body

您还可以删除app.use(bodyParser.urlencoded({extended:true}))

如果要使用
application/x-www-form-urlencoded
,请检查邮递员是否在标题选项卡中没有
application/json

否则bodyParser将抛出此错误


我认为您正在将主体类型设置为
x-www-form-urlencoded
,内容类型设置为
application/json
。能否将标题中的内容类型更改为
application/x-www-form-urlencoded
,并进行检查?我认为您正在将主体类型设置为
x-www-form-urlencoded
,内容类型设置为
application/json
。能否将标题中的内容类型更改为
application/x-www-form-urlencoded
,然后进行检查?