我应该将数据从应用程序发送到服务器,并且该应用程序的post方法是使用内容类型import express from 'express'
var http = require('http')
const redirectionRoutes = express.Router()
redirectionRoutes.use(function(req, res, next) {
req.rawBody = ''
req.headers['content-type'] = 'text/plain'
req.on('data', function(chunk) {
req.rawBody += chunk
})
req.on('end', function() {
next()
})
})
redirectionRoutes.post(/^\/update_services\/.*$/, function(request, response) {
var data = request.rawBody
var dataLength = data.length
var options = {
hostname: 'localhost',
port: 80,
path: request.path,
method: 'POST',
json: false,
headers: {
'Content-Type': 'text/plain',
'Content-Length': dataLength
}
}
var buffer = ''
var req = http.request(options, function(res) {
res.on('data', function(chunk) {
buffer += chunk
})
res.on('end', function() {
response.send(buffer)
})
})
req.write(data)
req.end()
})
创建的,但它是纯文本。我现在无法更新应用以更改此标头。当前应用程序正在工作,因为数据直接到达PHP并且PHP不会解析指定为json的传入数据。
at JSON.parse (<anonymous>)
at createStrictSyntaxError (../node_modules/body-parser/lib/types/json.js:157:10)
at parse (../node_modules/body-parser/lib/types/json.js:83:15)
at /Users/../node_modules/body-parser/lib/read.js:116:18
at invokeCallback (/Users/../node_modules/body-parser/node_modules/raw-body/index.js:224:16)
at done (/Users/../node_modules/body-parser/node_modules/raw-body/index.js:213:7)
at IncomingMessage.onEnd (/Users/../node_modules/body-parser/node_modules/raw-body/index.js:273:7)
at emitNone (events.js:105:13)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1047:12)
但是在nodejs(我的应用程序)中,由于内容类型被指定为json,正文解析器正在解析数据,因为它不是json,我收到错误:
SyntaxError:位于0位置的JSON中的意外标记#
{{1}}
nodejs / body解析器中是否有一种方法不解析这个传入的json,让它以纯文本形式进入函数。
答案 0 :(得分:0)
它解决了!! 我在应用程序代码的末尾和其他页面路由器一起导出此模块。因此,如果我没有在这个特定的路由器中使用,那么正在调用先前库中调用的正文解析器。