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.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)
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。