备个份, 原文: 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.

express新旧语法对比的更多相关文章

  1. spring加载配置新旧方式对比

    老方式 1.首先要配置配置文件,如beans.xml,内容如下: <?xml version="1.0" encoding="UTF-8"?> &l ...

  2. 使用Flexbox:新旧语法混用实现最佳浏览器兼容

    Flexbox非常的棒,肯定是未来布局的一种主流.在过去的几年这之中,语法改变了不少,这里有一篇“旧”和“新”新的语法区别教程(如果你对英文不太感兴趣,可以移步阅读中文版本).但是,如果我们把Flex ...

  3. mapreduce新旧api对比

    对比:hadoop版本1.x 新版,hadoop版本0.x 旧版 1.新api引用包一般是mapreduce ,旧版api引用的包一般是mapred 2.新api使用Job,旧版api使用JobCon ...

  4. SpringBoot2.X自定义拦截器实战及新旧配置对比(核心知识)

    简介: 讲解拦截器使用,Spingboot2.x新版本配置拦截拦截器和旧版本SpringBoot配置拦截器区别讲解 1.@Configuration 继承WebMvcConfigurationAdap ...

  5. Flex布局新旧混合写法详解(兼容微信)

    原文链接:https://www.usblog.cc/blog/post/justzhl/Flex布局新旧混合写法详解(兼容微信) flex是个非常好用的属性,如果说有什么可以完全代替 float 和 ...

  6. Flex布局新旧混合写法详解

    flex是个非常好用的属性,如果说有什么可以完全代替 float 和 position ,那么肯定是非它莫属了(虽然现在还有很多不支持 flex 的浏览器).然而国内很多浏览器对 Flex 的支持都不 ...

  7. Flex 布局新旧混合写法详解(兼容微信)

    flex 是个非常好用的属性,如果说有什么可以完全代替 float 和 position ,那么肯定是非它莫属了(虽然现在还有很多不支持 flex 的浏览器).然而国内很多浏览器对 flex 的支持都 ...

  8. 天气预报API(五):城市代码--“新编码”和“旧编码” 对比

    参考一些博客.文章 来查找 测试 接口,后来发现两套城市编码标准,有点想法,故拿来对比分析. 注:新旧编码是个人主观叫法,只是为了方便称呼,可能有不当之处,请留言更正. 暂且称 中国天气网等网站使用的 ...

  9. Matlab神经网络函数newff()新旧用法差异

    摘要 在Matlab R2010a版中,如果要创建一个具有两个隐含层.且神经元数分别为5.3的前向BP网络,使用旧的语法可以这样写: net1 = newff(minmax(P), [5 3 1]); ...

随机推荐

  1. BAPI 调用相当于BAPI_TRANSACTION_COMMIT 的方法

    为什么.net调用SAP的BAPI接口需要调用BAPI_TRANSACTION_COMMIT呢?首先得明白BAPI_TRANSACTION_COMMIT这个BAPI的作用.它功劳很大,在SAP里面很多 ...

  2. laravel 5.3 学习之路——路由(资源,别名)

    laravel的路由定义中,其中route:resoure(),可以直接定义类似restful风格的URL 例如:Route::resource('system/role','System\RoleC ...

  3. 在 ubuntu 下安装 apache 和 mod_mono ,并测试

    1. 保证 ubuntu 能联网. 2. 打开终端,输入:sudo apt-get install apache2 3. 安装完 apache2 后,打开浏览器,输入:http://localhost ...

  4. ActionBarSherlock SlidingMenu整合,解决SlidingMenu example的getSupportActionBar()方法不能用问题

    今天下载了SlidingMenu来研究,发现里面那个自带的example不能使用,总是提示BaseActivity 里面找不到getSupportActionBar()方法,到Github上面一查果然 ...

  5. .NET通用开发框架

    在开源中国社区,简单整理了下比较好的.NET通用开发框架.一个好的通用框架大概包括:开源.扩展性好.灵活性好.复用性好.维护性好.易测试.易发布.易部署.快速业务搭建(或业务集成).通用性强.参考资料 ...

  6. Hadoop入门进阶课程1--Hadoop1.X伪分布式安装

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博主为石山园,博客地址为 http://www.cnblogs.com/shishanyuan  ...

  7. Appium移动自动化测试(四)--one demo

    继续更新. -------------------------------------------- 第四节  安装Appium Client Appium Client是对webdriver原生ap ...

  8. django 快速实现完整登录系统(cookie)

    经过前面几节的练习,我们已经熟悉了django 的套路,这里来实现一个比较完整的登陆系统,其中包括注册.登陆.以及cookie的使用. 本操作的环境: =================== deep ...

  9. 远程方法调用(RMI)原理与示例

    RMI介绍 远程方法调用(RMI)顾名思义是一台机器上的程序调用另一台机器上的方法.这样可以大致知道RMI是用来干什么的,但是这种理解还不太确切.RMI是Java支撑分布式系统的基石,例如著名的EJB ...

  10. [linux笔记]理清linux安装程序用到的(configure, make, make install)

    我作为一名经常和linux打交道的程序员,每次在linux安装软件都祈求可以用——apt-get,yum,brew等应用程序管理器安装,有的时候事与愿违,你只能自己编译安装-wtf,说好的美丽世界呢? ...