Handling text/plain in Express (via connect)?
Asked Answered
S

6

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?

Skeie answered 19/9, 2012 at 14:58 Comment(0)
F
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.

Ferren answered 9/10, 2015 at 12:33 Comment(0)
A
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

Arteriotomy answered 19/9, 2012 at 15:22 Comment(1)
As @Ferren suggests (below), using: app.use(bodyParser.text()); works without the above code.Heathenism
T
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

}
Tenebrific answered 28/4, 2019 at 2:53 Comment(0)
S
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.

Swetiana answered 19/9, 2012 at 15:9 Comment(0)
S
2

You can parse every type to json via set type option

app.use(express.json({ type: ['text/*', '*/json'] }))
Selfforgetful answered 23/4, 2022 at 12:35 Comment(1)
best answer in 2024Alible
S
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'}); 
    }); 
}
Sloganeer answered 1/5, 2019 at 12:28 Comment(3)
This isn't really idiomatic express - best use bodyParser()Skeie
but it is in pure javascript, right? without any framework, you are right broSloganeer
Yes but it's an express question. See the title.Skeie

© 2022 - 2024 — McMap. All rights reserved.