码迷,mamicode.com
首页 > Web开发 > 详细

NodeJS with Express 4.x

时间:2014-07-22 23:09:32      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   文件   

 

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.jpg
var express = require("express"),
      app       = express();

app.use(/image,express.static(__dirname + /public));

 



 


Express 4.x 零碎知识点:

//输出静态文件
res.sendfile("/views/index.html");

//设定输出json
http.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, 激活cookie
app.use(cookieParser());

// 3, 在请求的req里获取cookies
JSON.stringify(req.cookies);

 


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

 
var cookieSession    = require(cookie-session);
app.use(cookieSession());




//清空session # Destroying a session
req.session = null



var 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)

 




NodeJS with Express 4.x,布布扣,bubuko.com

NodeJS with Express 4.x

标签:des   style   blog   http   color   文件   

原文地址:http://www.cnblogs.com/didi/p/3861319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!