开发环境:nodejs v0.11.13 + express 4.2.0 + mongodb v2.0.6
这里nodejs、express、mongodb的安装省去10000字。。。看安装后的package.json文件中依赖的中间件:
"dependencies": {
"express": "~4.2.0",
"express-session": "~1.3.1",
"mongodb": "~1.4.6",
"connect-mongodb": "~1.1.5",
"connect-multiparty":"~1.0.5",
"crypto": "~0.0.3",
"static-favicon": "~1.0.0",
"morgan": "~1.0.0",
"cookie-parser": "~1.0.1",
"body-parser": "~1.0.0",
"debug": "~0.7.4",
"ejs": "~0.8.5"
}
express 4.x.x已经去除connect,较express 2.x、express 3.x 一些功能的使用必须安装中间件才能正常使用。
这里简单介绍开发的环境中安装的中间件以及配置:
1、session功能
使用session功能可以采用内存存储,也可以中间件存储,比如:mongodb,需要的中间件:express-session/connect-mongodb,安装命令:
npm install express-session
npm install connect-mongodb
配置如下(已经和express2.x/3.x不一样了):
//采用connect-mongodb中间件作为Session存储
var session = require('express-session');
var Settings = require('./database/settings');
var MongoStore = require('connect-mongodb');
var db = require('./database/msession');
app.use(session({
secret: Settings.COOKIE_SECRET,
cookie: { maxAge: 600000 },
store: new MongoStore({
username: Settings.USERNAME,
password: Settings.PASSWORD,
url: Settings.URL,
db: db})
}));
2、文件上传下载功能
需要中间件:connect-multiparty,安装命令:npm install connect-multiparty,配置如下:
app.use(multiparty({uploadDir:'./temp', keepExtensions:true}));
3、加密
需要中间件:crypto,安装命令:npm install crypto,使用可直接require("crypto");
app.js文件:
var express = require('express'); var path = require('path'); var favicon = require('static-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); //文件上传下载中间件 var multiparty = require('connect-multiparty'); //后端处理逻辑 var routes = require('./routes/index'); var users = require('./routes/users'); //采用connect-mongodb中间件作为Session存储 var session = require('express-session'); var Settings = require('./database/settings'); var MongoStore = require('connect-mongodb'); var db = require('./database/msession'); var app = express(); //设置监听端口 app.listen(1001); //设置视图模版以及路径 app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); //装载中间件 app.use(favicon()); app.use(logger('dev')); app.use(bodyParser()); app.use(bodyParser.json()); app.use(bodyParser.urlencoded()); //设置上传文件的临时存储目录 app.use(multiparty({uploadDir:'./temp', keepExtensions:true})); app.use(cookieParser()); //session配置 app.use(session({ secret: Settings.COOKIE_SECRET, cookie: { maxAge: 600000 }, store: new MongoStore({ username: Settings.USERNAME, password: Settings.PASSWORD, url: Settings.URL, db: db}) })); //静态文件存放的根路径 app.use(express.static(path.join(__dirname, 'public'))); //请求路由转发设置 app.use('/', routes); app.use('/users', users); /// catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); /// error handlers // development error handler // will print stacktrace if (app.get('env') === 'development') { app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: err }); }); } // production error handler // no stacktraces leaked to user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: {} }); }); module.exports = app;
var Settings = require('./database/settings');引入的settings文件:
module.exports = { COOKIE_SECRET: 'TShare', URL: 'mongodb://127.0.0.1:27017/TShare', DB: 'TShare', HOST: '127.0.0.1', PORT: 27017, USERNAME: 'TShare', PASSWORD: 'TShare' };
var db = require('./database/msession');引入的msession文件:
var Settings = require('./settings'); var Db = require('mongodb').Db; var Server = require('mongodb').Server; var db = new Db(Settings.DB, new Server(Settings.HOST, Settings.PORT, {auto_reconnect:true, native_parser: true}),{safe: false}); module.exports = db;
以上仅作为备忘与分享,若有错误,请大家指正或讨论!