通过发布消息发送代码不起作用

Send code via post message is not working

本文关键字:代码 消息 不起作用 布消息      更新时间:2023-09-26

我想将代码发送到某个节点应用程序,我使用带有邮政消息的邮递员,并在正文中输入以下内容:

module.exports = function() {
    var express = require('express'),
        app = express();
    app.set('port', process.env.PORT || 3000);
    return app;
}

在我放置的请求的标题中

content-Type   application/text/enriched

在节点代码中我使用以下

module.exports = function (app) {
    fs = require('fs');
    var bodyParser = require('body-parser');
    ...
app.post('/bb',function(req,res){
        var fileContent = req.body

并且文件内容为空,我能够看到它工作,因为它在调试中停止

如果要

添加自定义内容类型,则需要记住两件事:

    内容类型不能是"应用程序/文本/扩充",
  1. 另一方面,"应用程序/文本扩充"是可以的。最多两个"字"。
  2. 您必须在正文分析器
  3. 配置上提供自定义接受标头,但正文分析器在使用自定义标头时会返回缓冲区

请参阅示例:

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'

我建议您尽量不使用自定义内容类型标头,因为事情更容易。我希望我的解释对您有所帮助。