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

    List Operators: sort reverse grep map my @castways = sort qw( first second third); # qw 给单词自动加上双引号 g ...

  2. Git 文件比较

    Git 的三个作业场: 工作区(Work Tree) 项目根目录下 .git 目录以外所有区域,是编辑项目文件的地方. 缓存区(Index) 工作区文件必须先保存在缓存区,之后从缓存区保存到仓库. 仓 ...

  3. linux2.6.24内核源代码分析(1)——扒一扒sk_buff

    最近研究了linux内核的网络子系统上的网络分组的接收与发送的流程,发现这个叫sk_buff的东西无处不在,内核利用了这个结构来管理分组,在各个层中传递这个结构,因此sk_buff可以说是linux内 ...

  4. linux 硬盘分区,分区,删除分区,格式化,挂载,卸载笔记

    linux 虽然一直都有在玩,但是对硬盘操作确实不是很熟悉今天有空,就整理了下. 1, 创建分区 先查看下是否有磁盘没有分区 fdisk -l 其中第一个框和第二个框,是已经分好区的磁盘,第三个硬盘没 ...

  5. set -x与set +x指令

    转载自:http://www.2cto.com/os/201304/205118.html 参考: http://blog.csdn.net/t0nsha/article/details/860688 ...

  6. 分享一套精美的现代 UI PSD 工具包【免费下载】

    艾库特·耶尔马兹,是土耳其伊斯坦布尔的一位高级艺术总监,他向大家分享了一套美丽的现代 UI 工具包,你可以免费下载.所以,你可以使用这个优秀的素材设计视觉互动和有吸引力的用户界面. 此 UI 套件提供 ...

  7. Android 学习笔记之Volley(七)实现Json数据加载和解析...

    学习内容: 1.使用Volley实现异步加载Json数据...   Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...

  8. Linux文件打包压缩、解压缩、备份命令使用方法(转载)

    对于刚刚接触Linux的人来说,一定会给Linux下一大堆各式各样的文件名给搞晕.别个不说,单单就压缩文件为例,我们知道在Windows下最常见的压缩文件就只有两种,一是,zip,另一个是.rar.可 ...

  9. 环信SDK与Apple Watch的结合(1)

    该系列是记录在apple watch上开发IM,用到了最近挺流行的环信IM SDK. 一.先来一段网上随处可查到的信息: 1.两种分辨率 1.65寸 312*390 1.5寸 272*340 2.开发 ...

  10. 【EF 译文系列】重试执行策略的局限性(EF 版本至少为 6)

    原文链接:Limitations with Retrying Execution Strategies (EF6 onwards) 当使用重试执行策略的时候,大体有以下两种局限性: 不支持以流的方式进 ...