Express是一個簡潔而靈活的 node.js Web套用框架, 提供一系列強大特性幫助你創建各種Web套用。
基本介紹
- 外文名:expressjs
- 屬於:node.js Web套用框架
- 功能:創建各種Web套用
- 性質:已有的特性進行二次抽象
性能
編程接口
var express = require('express');var app = express();app.get('/', function(req, res){ res.send('hello world');});app.listen(3000);
app.set('title', 'My Site');app.get('title');// => "My Site"
app.get('title');// => undefinedapp.set('title', 'My Site');app.get('title');// => "My Site"
app.enable('trust proxy');app.get('trust proxy');// => true
app.disable('trust proxy');app.get('trust proxy');// => false
app.enabled('trust proxy');// => falseapp.enable('trust proxy');app.enabled('trust proxy');// => true
app.disabled('trust proxy');// => trueapp.enable('trust proxy');app.disabled('trust proxy');// => false
// 所有環境app.configure(function(){ app.set('title', 'My Application'); })// 開發環境app.configure('development', function(){ app.set('db uri', 'localhost/dev'); })// 只用於生產環境app.configure('production', function(){ app.set('db uri', 'n.n.n.n/prod'); })
// 所有環境app.set('title', 'My Application');// 只用於開發環境if ('development' == app.get('env')) { app.set('db uri', 'localhost/dev'); } // 只用於生產環境 if ('production' == app.get('env')) { app.set('db uri', 'n.n.n.n/prod');}
var express = require('express');var app = express();// 一個簡單的 loggerapp.use(function(req, res, next){ console.log('%s %s', req.method, req.url); next();});// 回響app.use(function(req, res, next){ res.send('Hello World');});app.listen(3000);
// GET /javascripts/jquery.js// GET /style.css// GET /favicon.icoapp.use(express.static(__dirname + '/public'));
// GET /static/javascripts/jquery.js// GET /static/style.css// GET /static/favicon.icoapp.use('/static', express.static(__dirname + '/public'));
app.use(express.logger());app.use(express.static(__dirname + '/public'));app.use(function(req, res){ res.send('Hello');});
app.use(express.static(__dirname + '/public'));app.use(express.logger());app.use(function(req, res){ res.send('Hello');});
app.use(express.static(__dirname + '/public'));app.use(express.static(__dirname + '/files'));app.use(express.static(__dirname + '/uploads'));
- env 運行時環境,默認為 process.env.NODE_ENV 或者 "development"
- trust proxy 激活反向代理,默認未激活狀態
- json replacer JSON replacer 替換時的回調, 默認為null
- json spaces JSON 回響的空格數量,開發環境下是2 , 生產環境是0
- case sensitive routing 路由的大小寫敏感, 默認是關閉狀態, "/Foo" 和"/foo" 是一樣的
- strict routing 路由的嚴格格式, 默認情況下 "/foo" 和 "/foo/" 是被同樣對待的
- view cache 模板快取,在生產環境中是默認開啟的
- view engine 模板引擎
- views 模板的目錄
app.engine('jade', require('jade').__express);
app.engine('html', require('ejs').renderFile);
var engines = require('consolidate');app.engine('haml', engines.haml);app.engine('html', engines.hogan);
app.param('user', function(req, res, next, id){ User.find(id, function(err, user){ if (err) { next(err); } else if (user) { req.user = user; next(); } else { next(new Error('failed to load user')); } });});
app.param(function(name, fn){ if (fn instanceof RegExp) { return function(req, res, next, val){ var captures; if (captures = fn.exec(String(val))) { req.params[name] = captures; next(); } else { next('route'); } } }});
var express = require('express');var app = express();app.listen(3000);
var express = require('express');var https = require('https');var http = require('http');var app = express();http.createServer(app).listen(80);https.createServer(options, app).listen(443);
app.listen = function(){ var server = http.createServer(this); return server.listen.apply(server, arguments);};