Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling text/plain in Express (via connect)?

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?

like image 959
mikemaccana Avatar asked Sep 19 '12 14:09

mikemaccana


People also ask

Does Express use Connect?

Express uses Connect style middleware which is a common format for Node. js server frameworks. That means middleware written for Express can also be used in many other frameworks that implement Connect style middleware. A common middleware used with Express is cookie-parser .

What does Express () method do?

Express provides methods to specify what function is called for a particular HTTP verb ( GET , POST , SET , etc.) and URL pattern ("Route"), and methods to specify what template ("view") engine is used, where template files are located, and what template to use to render a response.

What is Express () in js?

Express is a node js web application framework that provides broad features for building web and mobile applications. It is used to build a single page, multipage, and hybrid web application. It's a layer built on the top of the Node js that helps manage servers and routes.


2 Answers

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.

like image 146
oozzal Avatar answered Oct 14 '22 14:10

oozzal


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

like image 31
TJ Holowaychuk Avatar answered Oct 14 '22 15:10

TJ Holowaychuk