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

es-restful

v2.3.1

Published

easy & simple nodejs restful server framework

Downloads

4

Readme

es-restful

easy & simple nodejs restful server framework

Build Status Coverage Status

用于快速构建 restful 服务器的框架, 基于typescript开发

特性

  • 基于typescript开发
  • API简单, 可以快速上手
  • 源码也简单, 可以随意扩展

快速上手

  1. 安装 npm install es-restful

  2. 编写服务器

    import { Restful, Resource } from 'es-restful';
    
    const api = new Restful();
    // 声明一个resource类
    class SayHello extends Resource {
        get({name}) {
          	return {
            	    data: `hello ${name}.`
             };
        }
    }
    
    api.addSource(SayHello, '/hello/<name>');
    api.start();
  3. 测试结果

    curl localhost:5050/hello/lleohao ==> hello lleohao

阅读快速指南可以更快上手

更完整的例子, 构建一个 todos 服务器

代码在 example 文件夹下, 运行前需要先编译

import { Parser, Restful, Resource } from '../lib/index';

const api = new Restful();

interface TodoItem {
    id: number;
    title: string;
    completed: boolean;
}

let TODOS: TodoItem[] = [{
    id: 0,
    title: 'init todo',
    completed: false
}];
let COUNT_ID = 0;


/**
 * 
 * 
 * @param {number} todoId
 */
let indexOf = function (todoId: number) {
    let index, len = TODOS.length;
    for (index = 0; index < len; index++) {
        if (TODOS[index].id === todoId) {
            break;
        }
    }

    if (index === len) {
        return -1;
    } else {
        return index;
    }
};


class Todo extends Resource {
    get({todoId}) {
        todoId = parseInt(todoId);
        let item = TODOS.filter((item) => {
            return item.id === todoId;
        });

        if (item.length === 0) {
            return {
                data: `The item for the id:${todoId} does not exist`,
                code: 404
            }
        } else {
            return {
                data: item[0]
            }
        }
    }

    delete({todoId}) {
        todoId = parseInt(todoId);
        let index = indexOf(todoId);

        if (index === -1) {
            return {
                data: `The item for the id:${todoId} does not exist`,
                code: 404
            }
        } else {
            TODOS.splice(index, 1);
            return {
                data: 'success'
            }
        }
    }

    put({todoId}) {
        todoId = parseInt(todoId);
        let index = indexOf(todoId);

        if (index === -1) {
            return {
                data: `The item for the id:${todoId} does not exist`,
                code: 404
            }
        } else {
            TODOS[index].completed = !TODOS[index].completed;
            return {
                data: 'success'
            }
        }
    }
}

class TodoList extends Resource {
    parser: Parser;

    constructor () {
        super();
        this.parser = new Parser();
        this.parser.addParam('title', {
            required: true,
            type: 'string'
        });
    }

    get() {
        return {
            data: TODOS
        };
    }

    @Resource.async()
    @Resource.addParser(this.parser)
    post({title}, _return) {
        setTimeout(() => {
            let item = {
                id: ++COUNT_ID,
                title: title,
                completed: false

            };
            TODOS.push(item);
            _return({
                data: item
            });
        }, 1000)
    }
}

api.addSource(TodoList, '/todos');
api.addSource(Todo, '/todos/<todoId>');

api.start({ debug: true });

测试结果

// 获取所有列表
curl localhost:5050/todos 
{"code":200,"message":"success","data":[{"id":0,"title":"init todo","completed":false}]}

// 获取指定id=0的todo详情
curl localhost:5050/todos/0
{"code":200,"message":"success","data":{"id":0,"title":"init todo","completed":false}}

// 新建 todo
curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: 3934a3b3-58b2-223 4-30be-6b213c722f23" -d '{ "title": "hh" }' http://localhost:5050/todos
{"code":200,"message":"success","data":{"id":1,"title":"hh","completed":false}}

// 删除id=0的todo
curl -X DELETE localhost:5050/todos/0
{"code":200,"message":"success","data":"success"}

// 更新id=1的todo状态
curl -X PUT localhost:5050/todos/1
{"code":200,"message":"success","data":"success"}

更详细的功能可以查阅快速指南