express新旧语法对比

备个份, 原文: http://stackoverflow.com/questions/25550819/error-most-middleware-like-bodyparser-is-no-longer-bundled-with-express

官方文档为: http://expressjs.com/guide/migrating-4.html

EDIT: I have posted a fork of Brian's seed with all the changes given below:https://github.com/LossyHorizon/angular-socket-io-seed

I found this discussion while fighting this my self. I am updating my copy of
https://github.com/btford/angular-socket-io-seed before I begin a new project. I plan to submit my changes back to Brian Ford when I figure out how.

OLD package.json

    "socket.io": "~0.9.16",
    "jade": "~0.31.2",
    "express": "~3.2.6"

NEW package.json

    "socket.io": "1.1.*",
    "jade": "1.6.*",
    "express": "4.8.*",
    "serve-favicon": "2",
    "morgan": "1.3.*",
    "method-override":"*", //added missing ,
    "body-parser": "1.8",
    "errorhandler": "*",
    "express-session": "*",
    "multer": "0.1.*"

You will need to explicitly add the middle ware that used to be just present, one line:

    var express = require('express');

Becomes a bunch of lines:

    var express = require('express');
    var favicon = require('serve-favicon');
    var logger = require('morgan');
    var methodOverride = require('method-override');
    var session = require('express-session');
    var bodyParser = require('body-parser');
    var multer = require('multer');
    var errorHandler = require('errorhandler');

OLD setup code:

    app.set('port', process.env.PORT || 3000);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.static(path.join(__dirname, 'public')));
    app.use(app.router);

NEW setup code:

    app.set('port', process.env.PORT || 3000);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(favicon(__dirname + '/public/favicon.ico'));
    app.use(logger('dev'));
    app.use(methodOverride());
    app.use(session({ resave: true, saveUninitialized: true, 
                      secret: 'uwotm8' }));

    // parse application/json
    app.use(bodyParser.json());                        

    // parse application/x-www-form-urlencoded
    app.use(bodyParser.urlencoded({ extended: true }));

    // parse multipart/form-data
    app.use(multer());

    app.use(express.static(path.join(__dirname, 'public')));

This is the part that took me forever. Sadly once I worked this out I looked in express/lib/application.js, searched for app.listen and there is a wonderful comment that explains things quite nicely. Not sure why I was so slow to look up the source, though it looks like I am not alone in that. 

Demos, docs, most blog posts, say you start your app with this, and it works, but locks us out from using socket.io (http & https at the same time also).

    app.listen(app.get('port'), function(){
       console.log('Express server on port ' + app.get('port'));
    });

This is functionally the same, but makes socket.io usage easy. Look up the source for app.listen() and you will see that this is what it is doing anyway.

    // could be/remain at top of file
    var http = require('http');    

    var server = http.createServer (app);
    // delete this line if NOT using socket.io
    var io = require('socket.io').listen(server);   

    server.listen(app.get('port'), function(){
       console.log('Express server on port ' + app.get('port'));
    });

I does not matter if you use the new express 'standard' syntax, or this syntax, the same http object is used either way, and as I recall http is a built in component to node.js anyway.

I hope this helps someone else.

posted @ 2015-07-08 02:02  $walker  阅读(425)  评论(0编辑  收藏  举报