0

with?

I get this error ...

connect deprecated multipart: use parser (multiparty, busboy, formidable) npm module instead node_modules/connect/lib/middleware/bodyParser.js:56:20 connect deprecated limit: Restrict request size at location of read node_modules/connect/lib/middleware/multipart.js:86:15

when I'm working on this tutorial here:

https://code.tutsplus.com/tutorials/introduction-to-express--net-33367

and the comments say not to use this. And of course the error goes away when I remove this statement

Once again the bad code is here:

app.use(express.bodyParser());

The full code is here:

var express = require('express');
var hbs = require('hbs');
var path = require('path');
var blogEngine = require('./blog');

var app = express();

var path_index = '../web_arcmarks/packet.html';
var path_index_resolved = path.resolve(path_index);


app.set('view engine', 'html');
app.engine('html', hbs.__express);
app.use(express.bodyParser());
app.use(express.static('public'));


// add templates

app.get('/', function(req, res) {
    res.render('index',{title:"My Blog", entries:blogEngine.getBlogEntries()});
});

app.get('/about', function(req, res) {
    res.render('about', {title:"About Me"});
});

app.get('/article/:id', function(req, res) {
    var entry = blogEngine.getBlogEntry(req.params.id);
    res.render('article',{title:entry.title, blog:entry});
});

app.listen(3000);

blog.js looks like this:

var entries = [
{"id":1, "title":"Hello World!", "body":"This is the body of my blog entry. Sooo exciting.", "published":"6/2/2013"},
{"id":2, "title":"Eggs for Breakfast", "body":"Today I had eggs for breakfast. Sooo exciting.", "published":"6/3/2013"},
{"id":3, "title":"Beer is Good", "body":"News Flash! Beer is awesome!", "published":"6/4/2013"},
{"id":4, "title":"Mean People Suck", "body":"People who are mean aren't nice or fun to hang around.", "published":"6/5/2013"},
{"id":5, "title":"I'm Leaving Technology X and You Care", "body":"Let me write some link bait about why I'm not using a particular technology anymore.", "published":"6/10/2013"},
{"id":6, "title":"Help My Kickstarter", "body":"I want a new XBox One. Please fund my Kickstarter.", "published":"6/12/2013"}];


exports.getBlogEntries = function() {
    return entries;
};

exports.getBlogEntry = function(id) {
    for(var i=0; i < entries.length; i++) {
        if(entries[i].id == id) return entries[i];
    }
};

1 Answers1

2

You are looking at an old tutorial that is not aimed at Express 4.0. In Express 4.0, the bodyparser was separated out of Express and is in a separate module. The formerly built-in one is now deprecated.

The code you show does not even appear to need the body-parser module at all. But, if you did have some code that needs it, then you should use the separate body-parser module:

var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: false }));

Note: there are multiple different middlewares that come in the body-parser module so you need to install the right ones that you need (this is just one example above). The doc for the body-parser module is here.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • @brannonteer - Express decided to be more of a core, small, fast, non-opinionated framework and let people add-on the components they most wanted to use. All the previously bundled components are still available, just as separate modules that can be used as needed and will not be in the way if not needed. No functionality was lost, it was just repackaged in a more component-friendly way and you have to explicitly add some things that were previously bulit-in. I wasn't there in the discussion, but I think the general idea was to keep Express lean and not let it get fat. – jfriend00 Mar 06 '17 at 01:48
  • @brannonteer - The body-parser module has nothing to do with rendering templates with handlebars. It is for parsing incoming requests (such as POSTs). – jfriend00 Mar 06 '17 at 01:49
  • @brannonteer - Also see this: [bodyParser is deprecated express 4](http://stackoverflow.com/questions/24330014/bodyparser-is-deprecated-/express-4) – jfriend00 Mar 06 '17 at 02:36
  • @brannonteer - If this answered your question, then you can indicate that to the community by clicking the green checkmark to the left of the answer. That will also earn your some reputation points for following the proper procedure here on stack overflow. – jfriend00 Mar 24 '17 at 04:20
  • Sure, the bigger issue I found is that I should not be using old tutorials ... living in the past ... can you recomend a short modern tutorial? Thanks. –  Mar 24 '17 at 18:46
  • Well, the Express website is where I would start. I'm not sure what else you are looking for. If you look on NPM (or the Github repository link from there), you will always find the doc to the latest version for any module so you can be assured you're not looking at old info. – jfriend00 Mar 24 '17 at 19:16