express随记01
系统变量的设置
app.get(env) | process.env.NODE_ENV: 会自动判断当前环境类型;app.get(port) | process.env.PORT: 必须手动设置;
app.param([name], callback)
- 用来处理多个匹配
app.param('id', function (req, res, next, id) {
console.log('CALLED ONLY ONCE');
next();
})
app.get('/user/:id', function (req, res, next) {
console.log('although this matches');
next();
});
app.get('/user/:id', function (req, res) {
console.log('and this matches too');
res.end();
});
cookie
- 其默认值:
{ path: '/', httpOnly: true, secure: false, maxAge: null }
//注意获取和设置的不同
app.get('/', function (req, res) {
if (!req.cookies.counter)
res.cookie('counter', 0);
else
res.cookie('counter', parseInt(req.cookies.counter,10) + 1);
res.status(200).send('cookies are: ', req.cookies);
})
- 签名加密的
cookie
//会自动加密解密处理
app.use(cookieParser('test-sign'));
.........
app.get('/', function (req, res) {
if (!req.signedCookies.counter)
res.cookie('counter', 0, {signed: true});
else
res.cookie('counter', parseInt(req.signedCookies.counter,10) + 1, {signed: true});
res.status(200).send('cookies are: ', req.signedCookies);
})
session
//将session保存到redis中
var cookieParser = require('cookie-parser');
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
app.use(cookieParser());
app.use(session({
resave: true, //是否强制保存session。即使没有被修改;
saveUninitialized: true, //是否强制保存不是初始化的session;
name: 'connect.sid', //默认浏览器cookie保存的名称;
store: new RedisStore({
host: 'localhost',
port: 6379
}),
secret: '0FFD9D8D-78F1-4A30-9A4E-0940ADE81645',
cookie: { path: '/', maxAge: 3600000 }
}));
app.get('/', function(req, res){
console.log('Session ID: ', req.sessionID)
if (req.session.counter) {
req.session.counter = req.session.counter +1;
} else {
req.session.counter = 1;
}
res.send('Counter: ' + req.session.counter)
});
其中要控制的实效包括:
ttl:session字段保存在数据库的实效;maxAge:cookie字段的有效时间;如果设置
cookie secure: true,那其只会在https时session才会去判断cookie值;session默认是httpOnly:true一般要将
saveUninitialized设为false,避免未对session修改也被保存下来;express-session自带debug模式,运行时设置DEBUG=express-session开启;一般流程
//登陆
login: fetching->no-session-found->[set]->saveing->split response->set-cookie
[redirect]-> fetching->session-found->touching->split response->touched
//重新打开
reopen: fetching->session-found->touching->touched
//退出
logout: fetching->session-found->[set]->no-session->
[redirect]->fetching->no-session-found
全局参数
//两种形式
app.locals = {};
res.locals = {};
特殊的输出
- 设置头部
app.get('/set-csv', function(req, res) {
var body = 'title, tags\n' +
'Practical Node.js, node.js express.js\n' +
'Rapid Prototyping with JS, backbone.js node.js mongodb\n' +
'JavaScript: The Good Parts, javascript\n'
res.set({'Content-Type': 'text/csv',
'Content-Length': body.length,
'Set-Cookie': ['type=reader', 'language=javascript']});
res.end(body);
});
- 设置响应码
res.status(200).end();
res.status(200).send();
res.status(200).json();
- 数据流输出文件
app.get('/stream2', function(req, res) {
var stream = fs.createReadStream(largeImagePath);
stream.on('data', function(data) {
res.write(data);
});
stream.on('end', function() {
res.end();
});
});
app.get('/stream1', function(req, res) {
var stream = fs.createReadStream(largeImagePath);
stream.pipe(res);
});
服务器开关
var server = http.createServer(app);
var boot = function () {
server.listen(app.get('port'), function(){
console.info('Express server listening on port ' + app.get('port'));
}); };
var shutdown = function() {
server.close();
};
if (require.main === module) {
boot();
} else {
console.info('Running app as a module');
exports.boot = boot;
exports.shutdown = shutdown;
exports.port = app.get('port');
}
express随记01的更多相关文章
- css随记01编辑技巧,背景与边框
代码优化 一个按钮的例子,使其值同比例变化; button{ color: white; background: #58a linear-gradient(#77a0bb, #58a); paddin ...
- nodejs随记01
EventEmitter var stream = require('stream'); var Readable = stream.Readable; //写入类(http-req就是),初始化时会 ...
- SpringBoot2.x-笔记(01)
程序入口 @SpringBootApplication public class SpringbootApplication { public static void main(String[] ar ...
- PBOC规范研究
一.ISO14443协议和PBOC关于CID的约定 看过协议的人其实都明白,RATS命令中参数字节的低半字节是CID,期中,CID不能为15. ISO14443协议中要求当RATS命令的CID等于0时 ...
- POS 60域用法
版权声明:本文为博主原创文章,未经博主允许不得转载. 自定义域(Reserved Private) 1.变量属性 N...17(LLLVAR),3个字节的长度值+最大17个字节的数字字符域. 压缩时用 ...
- Express 教程 01 - 入门教程之经典的Hello World
目录: 前言 一.Express?纳尼?! 二.开始前的准备工作 三.测试安装之经典的Hello World 四.使用express(1)来生成一个应用程序 五.说明 前言: 本篇文章是建立在Node ...
- [BI项目记]-TFS Express备份和恢复
在项目中对TFS进行备份操作是日常重要的工作之一,此篇主要描述如何对TFS Express进行备份,并且在另外一台服务器上进行恢复. 以下是操作的几个关键点: 备份数据库,在TFS管理工具中就可以完成 ...
- NodeJS+express+mogondb学习笔记01
0.准备工作 安装nodejs环境 官网地址:https://nodejs.org/en/ 下载好了 直接一路安装 也没有什么可以说的 不得不说nodejs对于新手上手还是很友好的,再加上现在n ...
- nodejs弯路-01之'express' 不是内部或外部命令
最近正想用node+angular+mongodb来完成一个小项目,三样都算是从零开始学习吧. 一开始是想用express -e projectname去创建一个ejs模板的项目.(一两句话就可以把大 ...
随机推荐
- codeforces 496B. Secret Combination 解题报告
题目链接:http://codeforces.com/problemset/problem/496/B 题目意思:给出 n 位数你,有两种操作:1.将每一位数字加一(当某一位 > 9 时只保存个 ...
- UVA 10815 Andy's First Dictionary ---set
题目链接 题意:输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出.单词不区分大小写. 刘汝佳算法竞赛入门经典(第二版)P112 #include <iostream> ...
- tableView滚到最后一行
dispatch_async(dispatch_get_main_queue(), ^{ [_tableview scrollToRowAtIndexPath:[NSIndexPath indexPa ...
- LTS学习
下载 例子 安装部署zookeeper 运行根目录下的sh build.sh或build.cmd脚本 --> 会在dist目录下生成lts-{version}-bin文件夹(bin里有启动命令) ...
- java https tomcat 单双认证(含证书生成和代码实现) 原创转载请备注,谢谢O(∩_∩)O
server: apache-tomcat-6.0.44 jdk1.7.0_79client: jdk1.7.0_79 jks是JAVA的keytools证书工具支持的证书私钥格式. pfx是微软支持 ...
- Git 操作的一些场景
1. 某些不需要的文件/文件夹,如:/build 之类,在添加对应的gitignore之前Push了,导致每次编译都会产生新的文件 解决方法:直接删掉不需要的文件/文件夹,然后push gitigno ...
- 四、优化及调试--网站优化--SEO在网页制作中的应用
SEO分类:白帽SEO.黑帽SEO 白帽SEO: 内容上的SEO: 网站标题.关键字.描述 网站内容优化 Robot.txt文件 网站地图 增加外链引用 前端SEO: 网站结构布局优化 扁平化结构(一 ...
- Deci and Centi Seconds parsing in java
http://stackoverflow.com/questions/14558663/deci-and-centi-seconds-parsing-in-java
- Sexagenary Cycle(天干地支法表示农历年份)
Sexagenary Cycle Time Limit: 2 Seconds Memory Limit: 65536 KB 题目链接:zoj 4669 The Chinese sexagen ...
- 攻城狮在路上(壹) Hibernate(八)--- 映射Hibernate组成关系
一.使用组成关系的原则: 在不导致数据冗余的前提下,尽可能减少数据库表的数目及表之间的外键参照关系,因为建立多个表的连接是很耗时的操作. 举例说明:Customer类中的Address属性,可以通过组 ...