npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

shuyar-rest-session

v1.0.8

Published

rest api session management

Downloads

5

Readme

#shuyar-rest-session

此中间件主要针对使用restify的API server的session管理。
API server主要是对外提供接口,客户端可能是web,H5, 也可能是mobile。由于mobile的app不支持
cookie,无法使用cookie来进行session的交互,所以此中间件仅使用header来进行session的管理。

web客户端,可以在web server层使用cookie来进行session的管理,与后台API交互的时候仍然使用header。

任意API的请求都会产生一个sid, 并在header里返回。

此中间件使用redis进行session的存储


在req中添加了方法:

  • req.saveSession 用来保存session数据到redis,比如用户登录后需要把相关信息存储到session里面。
  • req.destorySession 用来删除当前session数据,比如用户注销要删除session。
  • req.regenerateSession 删除旧session并生成新的session。比如用户登录的时候需要重新生成session。

安装

npm install shuyar-rest-session

使用

var session = require('shuyar-rest-session');

var session = session.config({
    redis: {
        host: '127.0.0.1', // 默认值127.0.0.1
        port: 6379, // 默认值6379
        db: 3 // 默认值undefined
        user: 'username' // 默认值undefined
        pass: 'password' // 默认值undefined
    },
    session: {
        ttl: 60 * 10, // 默认值30分钟
        header: 'SESSIONID' // 默认header为SESSION-ID
    },
    logger: logger // 默认值console
});

server.use(session);

程序会对每一个请求检查是否有sid,如果没有则生成一个新的sid,同时保存到redis并赋值给req.session:

req.session = {
    sid: 'sid'
}

对已有sid的请求,会在收到请求的时候进行session刷新

  • req.saveSession(sid, sessionData, callback)
callback(err, {succcess: true, status: 1});
  • req.destorySession(sid, callback)
callback(err, {succcess: true, status: 1});
  • req.regenerateSession(sid, callback)
callback(err, {succcess: true, sid: sid});

version 1.0.4 修复一些bugs

version 1.0.6 如果返回的是json对象,则增加sessionId