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 🙏

© 2026 – Pkg Stats / Ryan Hefner

nrest

v1.0.4

Published

基于Node.js的 REST API 框架

Downloads

15

Readme

简介

NRest 是一个基于Node.js的简易的Restful框架,主要完成了API路由配置与Request和Response对象扩展,更多功能正在开发当中。NRest的初衷是为单页面web应用或者其他平台应用提供统一HTTP服务接口的框架,其理想的应用场景为前后端全分离,因此可以看到这个框架没有对静态资源(HTML,CSS,JS)请求的支持。推荐将静态资源托管给更专业的服务或技术去做,NRest处理业务逻辑。

NRest 是一个同步的框架,所有的处理都应在规定时间内被完成。

NRest 是代码逻辑的一种组织形式,实际上并没有为开发者提供多少便利。

NRest 的所有核心逻辑都围绕Request与Response两个对象。

理念

运行时不变的数据都应在配置里。 对象功能的扩展来源于工厂而不是继承。

install

npm 安装

npm nrest --save

源码安装

git clone https://git.oschina.net/zmp0/NRest.git

快速入门

var rest = require('nrest');
rest.conf = {   //配置 NRest 框架大多数的功能配置都写在该配置里
    prot:80,    //监听端口
    rest:{      //restful 接口
        '/logic:':{
            GET: {
                hello: helloHandle //业务逻辑函数 请求地址:/logic:hello
            }
        }
    }
};

function helloHandle(request,response){
    response.end("hello");
}

rest.listen();
    

配置多个rest接口

var rest = require('nrest');
rest.conf = {   //配置 NRest 框架大多数的功能配置都写在该配置里
    prot:80,    //监听端口
    rest:{      //restful 接口
        '/logic:':{
            GET: {
                hello: helloHandle //业务逻辑函数 请求地址:/logic:hello
            },
            POST:{
                goods:goodsHandle  //业务逻辑函数 请求地址 /logic:goods 仅处理post响应
            }
        },
        '/user:':{
            GET:{
                loginOut:loginOutHandle //业务逻辑函数 请求地址 /user:loginOut
            },
            POST:{
                login:loginHandle //业务逻辑函数 请求地址 /user:login 仅处理post响应
            }
        }
    }
};

//业务逻辑
function helloHandle(request,response){
    response.end("hello");
}

function loginHandle(request,response){
    //TODO
}

function goodsHandle(request,response){
    //TODO
}

function loginOutHandle(request,response){
    //TODO
}

rest.listen();
    

所有的业务逻辑都是一个RFunction,其原型如下,第一个参数代表request,第二个参数代表response,支持node.js原生对象的所有属性与方法,新增session与cookies相关方法

function handle(request,response){};

static字段中的KVObject.key 为请求的资源,将与request.url.pathname 做对比,相同则读取KVObject.value中对应的静态文件作为相应返回。

Cookies支持

所有的业务逻辑中的Request添加了cookies属性,为response对象新增setCookie方法

conf字段属性

  • port: 监听端口 Number
  • rest: restful接口 KVObject(Value=KVObject)
  • extends: 扩展接口

KVObject为一个键值表对象,如下

var kvobj = {
    key0:'value0',
    key1:'value1',
    key2:'value2',
    keyn:'valuen'
};

所有的业务逻辑都是一个RFunction,其原型如下,第一个参数代表request,第二个参数代表response,支持node.js原生对象的所有属性与方法,新增session与cookies相关方法

function handle(request,response){};

static字段中的KVObject.key 为请求的资源,将与request.url.pathname 做对比,相同则读取KVObject.value中对应的静态文件作为相应返回。

扩展

NRest框架允许通过添加conf.extends字段来实现部分扩展功能


var rest = require('nrest');
rest.conf = {   //配置 NRest 框架大多数的功能配置都写在该配置里
    prot:80,    //监听端口
    rest:{      //restful 接口
        '/logic:':{
            GET: {
                hello: helloHandle //业务逻辑函数 请求地址:/logic:hello
            }
            _init_:function(request,response){return true};     //分组访问预处理 可以没有 返回true则继续具体的业务逻辑,返回false则直接返回不处理业务。 可作为权限或安全性验证
        }
    },
	extends:{
		afterAllRequest: function (request, response) {}, //所有请求处理之后
        beforeAllRequest: 'function', //所有请求处理之前
	}
};

function helloHandle(request,response){
    response.end("hello");
}

rest.listen();

Cookies使用

你可以在业务逻辑中这样使用Cookies

function helloHandle(request,response){
    response.setCookie("name","zmp");   //write cookies
    request.cookies.name;       //read cookies
}

Session使用

所有的业务逻辑中为request对象新增session属性,你可以这样使用

function helloHandle(request,response){
    request.session['name'] = 'zmp';   //write session
    request.session['name'];       //read session
}

推荐的使用方式

作为Restful API 作者希望将功能相似的请求处理作为一个模块来开发,写在一个模块中,然后导出,如下:

//logic.js
function login(request,response){};
function loginOut(request,response){};
function getVcode(request,response){};
//export
module.exports.login = login;
module.exports.loginOut = loginOut;
module.exports.getVcode = getVcode;

再在配置文件中声明其处理逻辑

const logic = require('./logic.js');
var rest = require('nrest');
rest.conf = {   //配置 NRest 框架大多数的功能配置都写在该配置里
    prot:80,    //监听端口
    rest:{      //restful 接口
        '/logic:':{
            GET: {
                login: logic.login,
				loginOut:logic.loginOut,
				getVcode:logic.getVcode
            }
        }
    }
};

function helloHandle(request,response){
    response.end("hello");
}