ExpressJS 4.0的新功能

來源:互聯網
上載者:User

標籤:style   blog   class   code   java   c   

ExpressJS是nodejs的web開發架構,最新版本為4.0相比於3.x版本,ExpressJS 4.X版本做了一些如下改變:

1. 去除了中介軟體的綁定,所有中介軟體的使用必須單獨下載,然後載入,這樣設計的好處在於以後使用者可以根據自己的需要按需下載項目所需要的中介軟體以及更新.

 

var app = express();//3.X版本,中介軟體的使用app.use(express.bodyParser());app.use(express.cookieParser());//4.0版本,需要單獨載入中介軟體var cookieParser = require(‘cookie-parser‘);var bodyParser = require(‘body-parser‘);//useapp.use(cookieParser());app.use(bodyParser()):

 

2.app.configure()不在有效,基於環境的參數配置只要通過簡單的if-else語句塊,例如開發環境的配置:

//expressjs 3.xapp.configure(‘development‘  , function({    //to do}));//expressjs 4.0var env = process.env.NODE_ENV || ‘development‘ ;if(‘development‘ == env ){    //to do};

 

3.路由的改變.

 expressJs 3.x路由的處理,一般為:

 

app.get(‘/‘  , function( res,  res ,next ){    // todo});app.post(‘/users‘ , function(res  ,res ,next){});

expressjs4.0添加了很多路由的可選方式,例如通過app.Router執行個體

var app = express();var router = express.Router();router.get(‘/‘ , function( req, res){..});router.post(‘/users‘,function(req,res){...});//調用路由 ,可以指定根目錄app.use(‘/‘  , router );

 

更多參考 http://scotch.io/bar-talk/expressjs-4-0-new-features-and-upgrading-from-3-0

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.