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-wangyu-server-static

v1.0.4

Published

1. lib目录为ServerStatic的核心库 2. example目录提供了ServerStatic的一些使用示例 3. bin目录为ServerStatic的命令行工具

Downloads

3

Readme

Node手写的静态资源服务中间件,更好的部署web资源服务,下发相关静态资源

  1. lib目录为ServerStatic的核心库
  2. example目录提供了ServerStatic的一些使用示例
  3. bin目录为ServerStatic的命令行工具

Using npm:

npm install node-wangyu-server-static -g 

Once the package is installed, you can import the library using require approach:

ServerStatic --port 80 --index index.js --rootName ./

Starting up http-server, serving ./
Available on:
http://192.168.121.36:80
http://127.0.0.1:80
pathRewrite

在前端History Router时,用户手动刷新页面会向后端发送请求。

  • pathRewrite:1 时,'/'下面无法正常访问的路径都会重定向到'/index.html'
  • pathRewrite:2 时,'/***/'下面无法正常访问的路径都会重定向到'/index.html'
  • ......
 fs.stat(pathJoin, (err, stat) => {
            if(err) {//路径错误没有资源
                //_ispathRewrite开启后强制重写路径
                if(this._pathRewrite && !this._ispathRewrite) {
                    this.pathname = path.join(this._pathRewriteFn(pathname), index)
                    this._ispathRewrite = true //已经重写路径,下一次pipe会直接抛出错误
                    this.pipe(res)
                } else {
                    this._emitError(404, err)
                } 
            } else {
                if(stat.isFile()) {
                    this.send(pathJoin, stat)
                } else {
                    this.pathname = path.join(pathname, index) //重写路径
                    this.pipe(res)
                }        
            }
        })
Example:

http模块创建服务,new ServerStatic实例后通过pipe方法下发静态资源

const server = http.createServer((req, res) => {
    let { pathname } = url.parse(req.url); 
    const serverStatic = new SeverStatic(req, pathname, {
        rootName: __dirname, //静态资源根路径
        // index: 'index.js', //访问'/'路径时定向的文件,默认为index.html
        // maxAge: 60, //强缓存cacheControl的时间,默认为10s

        //2级根路径重写,默认重定向到重写根路径下index.html,用于History Router下用户刷新页面后重定向到index.html
        // pathRewrite: 2, 
        
        // gzip: true //开启Gzip压缩,默认不开启
    })
    serverStatic.pipe(res)
    serverStatic.on('error', (err) => {
        console.log(err)
    })
})

express框架创建服务,作为一个下发静态资源的中间件使用

const express = require('express')
const path = require('path')
const serverStatic = require('node-wangyu-server-static')
let app = express()

app.use(serverStatic(path.resolve(__dirname, 'cache')))

app.listen(80, () => {
    console.log('listening 80 ......')
})