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

nice-road

v0.0.13

Published

create a simple server

Downloads

182

Readme

How to use

npm install nice-road

example

your-project

  1. main.js
  2. setting.js
  3. src/router

main.js

const setting = require('./setting');
const { NiceRoad, multipleValidate, ruleBreak, ruleNext } = require('nice-road');
const road = new NiceRoad(setting);
road.setRule(rule);
road.initRouter('./src/router');
road.run(8080);

/**
 * 规则函数
 * @param rules 规则列表,在路由中配置的规则,如['GET']
 * @param option 配置参数,{token, method, jwt_key, rsa_private_pem}
 */
function rule(rules, option) {
  return multipleValidate([
    [rules.includes('GET') && option.method !== 'GET', '请求方法错误'],
    [rules.includes('POST') && option.method !== 'POST', '请求方法错误'],
    () => {
      if (rules.includes('user')) {
        // 执行用户校验, 如果失败,则返回 ruleBreak('用户校验失败')
        return ruleBreak('用户校验失败');
      }

      return ruleNext('验证通过');
    }
  ]);
}

setting.js

const { applySetting } = require("nice-road");
//配置静态资源路径
const staticPath = 'E://static';

//配置链接数据库参数
const mysqlConfig = {
  host: '127.0.0.1',
  port: 3360,              //端口号
  database: 'database',    //数据库名
  username: 'root',        //数据库用户名
  password: '***********', //数据库密码
  timezone: '+08:00'       //设置时区
};

applySetting({
  staticPath,
  mysqlConfig,
});

router文件夹 src/router/***.js

/**
 * router文件夹下面可以创建多个文件,每个文件按如下格式编写
 * 引入npath函数创建路由
 * @returns {urls: Array, rules: Array}
 * */

const { npath } = require('nice-road');

function getUserAvatar(req, res) {
  res.sendFile('C:/Users/admin/Desktop/avatar.jpg');
}

function getUserList(req, res) {
  res.send('your can get userList');
}

// 全局规则
const rules = ['GET'];
// 路由配置
const urls = [
  // 不设置规则,则使用全局规则
  npath('/getUserAvatar', getUserAvatar),
  // 使用设置的规则
  npath('/getUserList', getUserList, ['POST', 'user'])
];

module.exports = {
  urls,
  rules
};