users.json

{
"tobi": {
"password": "ferret",
"name": "Tobi Holowaychuk"
}
}

session.js

var connect = require('connect')
var users = require('./users') var server = connect(
connect.logger('dev'),
connect.bodyParser(),
connect.cookieParser(),
connect.session({secret: 'my app secret'}),
function(req, res, next) {
if ('/' == req.url && req.session.logged_in) {
res.writeHead(200, {'Content-Type': 'text/html'})
res.end('Welcome back, ' + req.session.name + '<a href="/logout">Logout</a>')
} else {
next()
}
},
function(req, res, next) {
if ('/' == req.url && 'GET' == req.method) {
res.writeHead(200, {'Content-Type': 'text/html'})
res.end([
'<form action="/login" method="POST">',
'<input type="text" name="user">',
'<input type="password" name="password">',
'<button>Submit</button>',
'</form>'
].join(''))
} else {
next()
}
},
function(req, res, next) {
if ('/login' == req.url && 'POST' == req.method) {
res.writeHead(200)
if (!users[req.body.user] || req.body.password != users[req.body.user].password) {
res.end('Bad username/password')
} else {
req.session.logged_in = true
req.session.name = users[req.body.user].name
res.end('Authenticated!')
}
} else {
next()
}
},
function(req, res, next) {
if ('/logout' == req.url) {
req.session.logged_in = false
res.writeHead(200)
res.end('Logged out')
} else {
next()
}
}
) server.listen(3000)

【session】的更多相关文章

  1. 【cookie的使用】&【Session】

    明确一点:        cookie由服务器创建Cookie cookie=new Cookie("haha","xixi") 通过HtttpServletR ...

  2. 【Session】Tomcat中Session的外置

    > 参考的优秀文章 Tomcat Session 持久化 Package org.apache.catalina.session 最近同事在做Session外置的功能,我对Session持久化. ...

  3. 【Session】Tomcat中Session持久化到文件系统或数据库

    参考的优秀文章 Tomcat Session 持久化 Package org.apache.catalina.session 最近同事在做Session外置的功能,我对Session持久化.共享也不太 ...

  4. 【Tomcat】Tomcat Session在Redis共享

    参考的优秀文章 Redis-backed non-sticky session store for Apache Tomcat 简单地配置Tomcat Session在Redis共享 我使用的是现有的 ...

  5. 【转】MVC4验证用户登录特性实现方法

    在开发过程中,需要用户登陆才能访问指定的页面这种功能,微软已经提供了这个特性. // 摘要: // 表示一个特性,该特性用于限制调用方对操作方法的访问. [AttributeUsage(Attribu ...

  6. 【学习】018 Spring框架

    Spring的概述 Spring框架,可以解决对象创建以及对象之间依赖关系的一种框架. 且可以和其他框架一起使用:Spring与Struts,  Spring与hibernate (起到整合(粘合)作 ...

  7. 【转】ora-00031:session marked for kill处理oracle中杀不掉的锁

    一些ORACLE中的进程被杀掉后,状态被置为"killed",但是锁定的资源很长时间不释放,有时实在没办法,只好重启数据库.现在提供一种方法解决这种问题,那就是在ORACLE中杀不 ...

  8. PHP5 session 详解【经典】 -- 转帖

    PHP5 session 详解[经典] http协议是WEB服务器与客户端(浏览器)相互通信的协议,它是一种无状态协议.所谓无状态,指的是不会维护http请求数据,http请求是独立的,非持久的.而越 ...

  9. 【Spring】Spring IOC原理及源码解析之scope=request、session

    一.容器 1. 容器 抛出一个议点:BeanFactory是IOC容器,而ApplicationContex则是Spring容器. 什么是容器?Collection和Container这两个单词都有存 ...

随机推荐

  1. Masonry约束崩溃

    报错: Trapped uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint const ...

  2. iOS开发——JS网页交互——javaScript

    JS中调用OC #import "ViewController.h" @interface ViewController () <UIWebViewDelegate> ...

  3. html-----015---HTML ASCII 参考手册

    HTML 和 XHTML 用标准的 7 比特 ASCII 代码在网络上传输数据. 7 比特 ASCII 代码可提供 128 个不同的字符值. 7 比特 可显示的 ASCII 代码 <html&g ...

  4. 淘宝可以传照片搜索商品,verygood.雅客VC多味水果糖

    奶奶喜欢吃点硬糖.在当地买了些说是不好.到是一个亲戚买的一种糖比较满意(好久了都快融化了). 但是我只有照片,能知道品牌,在jd没这样一样的商品了. 还好借助淘宝的传照片功能,找到了.

  5. leetcode problem 31 -- Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. mod_wsgi

    配置: WSGIScriptAlias /var/www/wsgi-scripts/simple.wsgi def application(environ, start_response): outp ...

  7. ubuntu 初始

    1.命令行界面与图形界面 ctrl + alt + f1进入命令行界面 ctrl + alt + f7 切换图形界面 2.ubuntu 的wubi安装与卸载 第一:在win 系统下启动DOS,进入命令 ...

  8. 移动web经验积累

    1.从最小宽度时候开发,调试到iphone4来开发 2.宽度百分比,高度由具体内容决定, 3.文字需要设置最大高度,溢出隐藏 white-space: nowrap; text-overflow: e ...

  9. html锚点

    ID模式 <h3><a href="#start">开始</a></h3> <div> 你好 <b/> &l ...

  10. View和ViewGroup的区别 -- Touch事件处理

    View.java源码: /frameworks/base/core/java/android/view/View.java View.java的 dispatchTouchEvent 方法: 经过一 ...