8

I am using Express 3, and would like to handle text/plain POSTs.

Express 3 uses connect's bodyParser now (I think the old Express code got moved to connect). The documentation for bodyParser gives some details about how to make it support additional file types. And I found an excellent blog post about how handling text/plain was done in old versions of Express).

  • Should I explicitly require connect (and let node's require cache the modified version)? Or is connect exposed via express somewhere?

  • connect.bodyParser does not have a 'parse' key.

How can I make Express (via connect) handle text/plain POSTs?

Chenmunka
  • 685
  • 4
  • 21
  • 25
mikemaccana
  • 110,530
  • 99
  • 389
  • 494

6 Answers6

28

With bodyParser as dependency, add this to your app.js file.

var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.text());

Happy Noding.

oozzal
  • 2,441
  • 23
  • 21
26

https://gist.github.com/3750227

app.use(function(req, res, next){
  if (req.is('text/*')) {
    req.text = '';
    req.setEncoding('utf8');
    req.on('data', function(chunk){ req.text += chunk });
    req.on('end', next);
  } else {
    next();
  }
});

Will add the text as req.text

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
TJ Holowaychuk
  • 649
  • 5
  • 3
5

In express.js "^4.16..." the following code works fine for me:

// parse an HTML body as a string
app.use(bodyParser.text({ type: 'text/*' }))

The extended piece of the code is below:

// parse an HTML body as a string
app.use(bodyParser.text({ type: 'text/*' }))

// Enable CORS for ExpressJS
app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS')
  res.header('Access-Control-Allow-Credentials', true)
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Methods, Credentials')
  next()
})


// Api url 
app.post('/api/myApi', (req, res) => {

  const bodyJson = JSON.parse(req.body)
  // do something

}
Roman
  • 19,236
  • 15
  • 93
  • 97
2

I would just make a module similar to the json.js middleware module and just don't bother converting the buf data into anything else. Wrap it into a plain.js file, apply some decent "don't repeat yourself" refactoring, and submit a pull request to connect. Seems generally handy. However, note that while convenient, large enough request bodies will require streaming straight to disk at some point so you don't consume all the memory in your node server.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
1

You can parse every type to json via set type option

app.use(express.json({ type: ['text/*', '*/json'] }))
user6689187
  • 101
  • 3
0

You may try this :

var expressApi= (req, res,params)=>{
    console.log('req.body',params);
    var body = '';
    req.on('data', function (data) {
        body += data;
    });
    req.on('end', function () {
        res.write({status:200,message:'read data'}); 
    }); 
}
Kunvar Singh
  • 1,779
  • 2
  • 13
  • 21