我想将代码发送到某个 Node 应用程序,我使用 postman 发送消息,并在正文中放置以下内容:
module.exports = function() {
var express = require('express'),
app = express();
app.set('port', process.env.PORT || 3000);
return app;
}
在我放置的请求的 header 中
content-Type application/text/enriched
在 Node 代码中我使用以下
module.exports = function (app) {
fs = require('fs');
var bodyParser = require('body-parser');
...
app.post('/bb',function(req,res){
var fileContent = req.body
文件内容为空,我可以看到它可以工作,因为它在调试中停止
最佳答案
如果您想添加自定义内容类型,那么您需要记住两件事:
- 内容类型不能是“application/text/enriched”,而“application/text-enriched”则可以。最多两个“字”。
- 您必须在主体解析器配置上提供自定义接受 header ,但当您使用自定义 header 时主体解析器会返回一个缓冲区
参见示例:
var express = require('express')
var app = express()
var bodyParser = require('body-parser')
app.use(bodyParser.raw({ type: 'application/text-enriched' }))
app.post('/demo', function(req, res) {
console.log('POST DATA')
console.log('STREAM', req.body)
console.log('STREAM to STRING', req.body.toString())
res.status(200).send('ok');
});
app.listen(3000);
您可以使用curl在控制台中进行测试:
curl 'http://localhost:3000/demo' -d 'name=john&surname=doe' -H 'Content-Type: application/text-enriched'
我建议您尝试不使用自定义内容类型 header ,因为事情会更容易。希望我的解释对您有所帮助。
关于javascript - 通过帖子消息发送代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30839777/