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

express-token

v0.1.0

Published

Simple token authorization middleware for Express

Downloads

4

Readme

express-token

Token authentication for express.

前后端分离之后,一般会采用 rest api 类似方式来进行数据交互,此时如果用Session认证可能就不是那么合适(当然,也是可以的)。

由于我自己的多个项目都采用了token认证,每次都去实现一套token认证,也是蛮不爽的。so,搞个中间件玩起来。

How to use

# Install
npm i --save express-token

# Use
const expressToken = require('express-token');

app.use(expressToken(options)); // optional options

# When login
expressToken.login(userInfo)
.then(token => {
  // There can get token, you can use it do anything.
  console.log(token);
});

# When logout
expressToken.logout(token)
.then(() => {
  // Logout succeed,do something...
});

# Get token or user info

router.user('/', (req, res, next) => {
  let token = req.token; // Get token
  let user = req.user; // Get user info
  if(!req.user){
    // Not login
  }
});

Options(均为可选参数,默认无需设置)

  • ttl {Number} 单位毫秒,token滑动过期时间,默认30分钟
  • tokenHeader {String} 自定义的token header,默认 'x-token'(决定我们从哪个header获取token)
  • generateToken {(req: Request) => {}} 生成token的函数,允许返回Promise
  • getToken {(req: Request, tokenHeader: string) => {}} 获取token的函数,如果配置了该函数,将不会通过tokenHeader获取token。

示例:

let options = {
  ttl: 1000 * 60 * 60, // 1个小时
  tokenHeader: 'x-test-token', // 默认通过 req.headers['x-test-token'] 获取token
  generateToken(req){ // 自定义token生成函数
    return Math.random().toString(16);
  },
  getToken(req, tokenHeader){ // 自定义获取token
    return req.headers['xxx'];
  }
};

How to develop?

git clone

# Run dev
npm run dev

How to feedback?

If you find some bug or have some feedback, please create an issue.

ChANGELOG

Click here to show more change log.