首页 > 代码库 > NodeJS with Express 4.x

NodeJS with Express 4.x

 

Express 4.x 静态资源目录设置:

//静态文件目录app.use(/public, express.static(__dirname+/public));app.use(/data, express.static(__dirname+/data));

 

//可以让 http://www.domain.com/image/aaa.jpg访问到服务器public目录下的aaa.jpgvar express = require("express"),      app       = express();app.use(/image,express.static(__dirname + /public));

 



 


Express 4.x 零碎知识点:

//输出静态文件res.sendfile("/views/index.html");//设定输出jsonhttp.get("/haha/", function(req, res){    //設定HTTP Header    res.setHeader(Content-Type, application/json);    res.send({a:123,b:456});});//跳转res.redirect("http://google.com");//NodeJS显示当前目录:__dirname

 

 

Express里cookie操作的信息:
https://github.com/expressjs/cookie-parser

// 1, 加载cookie模块var cookieParser = require(cookie-parser);// 2, 激活cookieapp.use(cookieParser());// 3, 在请求的req里获取cookiesJSON.stringify(req.cookies);

 


Express里的 cookie-session
https://github.com/expressjs/cookie-session

 var cookieSession    = require(cookie-session);app.use(cookieSession());//清空session # Destroying a sessionreq.session = nullvar express = require(express)var session = require(cookie-session)var app = express()app.use(session({  keys: [key1, key2],  secureProxy: true // if you do SSL outside of node}))app.use(function (req, res, next) {  var n = req.session.views || 0  req.session.views = ++n  res.end(n +  views)})app.listen(3000)