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

libird

v1.0.2

Published

a light bird http server

Downloads

1

Readme

Libird

Light Bird Server,基于 Node.js 的 web 开发框架。

安装

新建目录或直接进入已有工作目录(假设目录名为 project

$ mkdir project
$ cd project

安装 libird

$ npm install libird

用法示例

下载安装完成后,在 project 目录中创建 libird.js 文件。

1. 静态资源服务器

//引入 libird 模块
var libird = require('libird');

//启动 http server, 默认监听本地8888端口号
libird.start();

// 修改端口号 比如监听本地8989端口号
// libird.start(8989);

运行 $ node libird.js,后台显示 server listening at:8888。 浏览器打开 http://127.0.0.1:8888 , 针对 / 的请求默认查找 libird.js 当前所在目录(即 project )下的 index.html 文件,如果未找到则显示 404 Not Found!

通过 HOST/FILEPATH 可以访问静态资源文件。例如

// 访问 project 目录下的 index.css 文件
http://127.0.0.1:8888/index.css  

//访问 project/pages 目录下的 list.html 文件
http://127.0.0.1:8888/pages/list.html 

可以为静态文件指定根目录

var libird = require('libird');  
libird.setDirPath('./app'); //设置根目录
libird.start();

重新运行 $ node libird.js。此时 http://127.0.0.1:8888 默认查找指定根目录(即app)下的 index.html 文件。

2. 自定义路由

路由基本结构 router.METHOD(PATH, HANDLER), 暂且支持GET/POST。基本用法如下:

GET

var libird = require('libird');
var router = libird.router;
router.get('/', function(req, res) {
    res.send('Hello World!');
});
router.get('/detail/:id', function(req, res) {
    res.send('req.params.id=' + req.params.id);
});
libird.start();

上述代码运行后将以 Hello World! 响应针对/的请求。
访问 http://127.0.0.1:8888/detail/100,页面显示 req.params.id=100

POST

目前支持的 POST 请求 Content-Type为 jsonx-www-form-urlencoded

var libird = require('libird');
var router = libird.router;
router.post('/login', function(req, res) {
    res.setCookie('userid', '111111'); //新增 cookie
    res.send(req.body); 
});
router.post('/logout', function(req, res) {
    res.clearCookie('userid'); // 清除 cookie
    res.send({"success": true});
});
libird.start();

运行 $ node libird.js。可以通过 Chrome 的插件 Postman 来模拟 POST 请求, 如下所示:

请求

POST /login HTTP/1.1
Host: 127.0.0.1:8888
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded

username=Lee&password=111

libird 将 POST 请求的主体存入req.body

响应

上述示例代码的响应即直接显示 req.body,

{"username":"Lee","password":"111"}

Cookie

  • 当请求/login的时候,新增名为userid的 cookie userid 130af17e178815982268491f088dabb3

  • 当请求/logout时清除名为 userid的 cookie。

  • 可通过req.getSession('userid')获取原始的 userid (即111111)。比如用于判断用户的登录状态。

     router.get('/user', function(req, res) {
        var userid = req.getSession('userid');
        var status = userid ? '已登录' : '未登录';
        res.send(status);
     });

--