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

node-fashion

v0.1.3

Published

a node http server framework.

Downloads

29

Readme

基于Express快速构建WEB应用服务的框架

Node-Fashion NPM downloads

安装

npm install --save node-fashion

选项

  • baseUrl: [String] - 默认/api
  • port: [Number] - 端口, 默认12321
  • debugger: [Boolean] - 调试模式, 默认false
  • logger: [Object] - 日志输出, 默认console

文档

  • route
    • url: 请求地址, 如'/users', '/users/:id'
    • method: HTTP请求方法, [OPTIONS|GET|POST|PUT|DELETE|...]
    • description: 描述信息
    • handle: 请求处理函数
      {
        url: '/users',
        method: 'GET',
        description: '获取用户列表',
        handle: function (req, res) {
          ...
          res.respond(null, [
            {id: 1, name: 'qwe'},
            {id: 2, name: 'aaa'},
          ])
        }
      }
  • server.handle([Array | Object])
    • 注册route, 可以传递一个route对象或route对象的数组
  • server.use()
    • 该方法等同于Express的use方法
  • server.start()
    • 启动服务
  • server.interceptors([Callback Function])
    • 简易路由拦截器, 携带四个参数req, res, route, next,
    • 其中route包含url,baseUrl,method,description四个属性
      server.interceptors(function (req, res, route, next) {
        console.log(route)
        next()
      })
  • server.beforeResponse([Callback Function])
    • 服务器响应客户端前的自定义回调函数, 你可以在这里做一些额外的工作, 默认null
    • 必须调用'res.respond()'方法, 该回调函数才会生效
    • 该回调接受三个参数(err, route, done)
      • err: 服务器处理返回的错误信息
      • route: 所请求的路由信息, 额外包含(params, body, originalUrl)信息
      • done: 必须调用done()方法来结束该回调函数
  • res.respond([Error Object | String], [Error Code | Object])
  // 调用通用响应处理函数
  function (req, res) {
    // ...
    // error - errorCode默认是`500`
    res.respond(new Error('error message'))
    res.respond(new Error('error message'), 401)
    res.respond('error message', 401)
    // success
    res.respond(null, [{id: 1, name: 'qwe'}, {id: 2, name: 'aaa'}])
  }

使用

users.js

module.exports = [
  {
    url: '/users',
    method: 'get',
    handle: function (req, res) {
      res.respond(null, [
        {
          id: 123,
          name: 'sdx'
        },
        {
          id: 234,
          name: 'dss'
        }
      ])
    }
  },
  {
    url: '/users/:id',
    method: 'get',
    handle: function (req, res) {
      res.respond(null, 'get users by id.')
    }
  },
  {
    url: '/users',
    method: 'post',
    handle: function (req, res) {
      res.respond(null, 'add a user.')
    }
  }
]

api.js

module.exports = [
  require('./users'),
  {
    url: '/hello-world',
    method: 'get',
    handle: function (req, res) {
      res.send('hello world.')
    }
  }
]

index.js

var Server = require('node-fashion')

var server = new Server()

// add middleware
server.use(function (req, res, next) {
  console.log('============[配置中间件]=============')
  next()
})

// add basic api
server.use('/hello', function (req, res) {
  console.log('create api by [/hello].')
  res.send('test success.')
})

// 统一接口函数处理
server.handle(require('./api'))

server.start()

作者