首页 > 代码库 > 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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。