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

koa-spa-server

v1.2.5

Published

单页应用服务器,静态资源及Api代理访问

Downloads

20

Readme

koa-spa-server 用法

const path = require('path');
const koaServer = require('koa-spa-server');
// const L = require('nirvana-logger')('example');
const port = process.env.PORT0 || 7002;

const proxyTable = {
  '/api': {
    target: 'http://localhost:8000',
    changeOrigin: false,
    logs: true,
  },
  '/openapi': {
    target: 'http://localhost:8000',
    changeOrigin: false,
    logs: true,
  },
};

// 创建服务
const server = new koaServer({
  port,
  proxyTable,
  fallback: true,
});

// 静态目录
server.setStatic('/', path.join(__dirname, '.'));
// 启动服务
server.start();

选项配置

| 配置项 | 说明 | | --- | --- | | port | 服务端口,默认 7002 | webRoot | 静态资源配置选项 {prefix: '前辍', root: '静态资源的绝对路径', maxage: 0, index: 'index.html'} | fallback | 是否启用 spa选项 默认 true | fallbackOption | object url重写选项配置 | compress | 压缩选项,默认启用 | proxyTable | 代理配置,参考 | healthCheck | 健康检查, true or false, 默认为 true, 支持 /health /healthCheck 或 /healthcheck | oAuth | object oAuth配置 | oAuth.url | string oAuth token url | oAuth.client_id | string client_id | oAuth.client_secret | string client_secret | oAuth.expires | number beartoken缓存时长,单位为秒 默认为 1200

webRoot配置项

  • root 静态资源的绝对路径
  • maxage 浏览器缓存 max-age 单位毫秒. 默认为 0 毫秒
  • hidden 是否允许隐藏文件. 默认 false
  • index 默认页 'index.html'
  • defer 如果为 true 则允许下游中间件优先处理.
  • gzip 如果为true 浏览器端支持gzip并且存在资源的.gz扩展默认为 true.
  • prefix url前辍,默认 '/'
  • suffix url后辍,默认 ''

fallbackOption配置项

index

索引页,默认为 'index.html'

rewrites url重写规则

使用自定义url匹配模式覆盖index选项,例如:

// fallbackOption配置项
{
    rewrites: [
            { from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') },
            // { from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') },
            { from: /^\/$/, to: '/index.html' },
            { from: /^\/(profile|app|work-order|config-server|instance|service|domain|log|oauth|cdn)/, to: '/profile.html' },
            { from: /^\/login/, to: '/login.html' },
            { from: /^\/user/, to: '/user.html' },
            { from: /^\/terminal/, to: '/terminal.html' },
            { from: /^\/docs/, to: '/docs.html' },
          ]
}

verbose

是否打印url重写信息

{
  verbose: true
}

example

// config.js

const path = require('path');

const targets = {
  openapi: {
    local: 'http://10.10.232.242:8000',
    test: 'http://10.10.232.242:8000',
    production: 'http://10.10.232.242:8000',
  },
  api: {
    local: 'http://10.10.232.242:9000',
    test: 'http://10.10.232.242:9000',
    production: 'http://10.10.232.242:9000',
  },
};

const env = process.env.NODE_ENV || 'local';

module.exports = {
  // 静态资源
  webRoot: {
    prefix: '/',
    root: path.join(__dirname, '../example'),
  },
  // url重写配置
  fallback: true,
  fallbackOption: {
    index: 'index.html',
    verbose: 'true',
    rewrites: [
        { from: /^\/$/, to: '/index.html' },
        { from: /^\/(profile|app|work-order|config-server|instance|service|domain|log|oauth|cdn)/, to: '/profile.html' },
        { from: /^\/login/, to: '/login.html' },
        { from: /^\/user/, to: '/user.html' },
        { from: /^\/terminal/, to: '/terminal.html' },
        { from: /^\/docs/, to: '/docs.html' },
    ]
  },
  // 代理配置
  proxyTable: {
    '/api/rong360': {
      target: targets.openapi[env],
      changeOrigin: true,
      logs: true,
      pathRewrite: path => path.replace('\/api\/', '\/'),
    },
    '/openapi': {
      target: targets.openapi[env],
      changeOrigin: true,
      pathRewrite: path => path.replace('\/openapi\/', '\/'),
    },
    '/api/(user|internal|bank|repay|payment|account|location|creditItem)': {
      target: targets.api[env],
      changeOrigin: true,
      logs: true,
      pathRewrite: path => path.replace('\/api\/', '\/puhui-nirvana-user\/'),
    },
    '/api/risk': {
      target: targets.api[env],
      changeOrigin: true,
      pathRewrite: path => path.replace('\/api\/risk/', '\/puhui-holmes-risk-assess/api/v2/'),
    },
    '/api/assets': {
      target: targets.api[env],
      changeOrigin: true,
      pathRewrite: path => path.replace('\/api\/assets\/', '\/puhui-core-server-cloud/api/'),
    },
  },
};

关于代理

http://forbeslindesay.github.io/express-route-tester/ https://github.com/component/path-to-regexp

路径正则验证

http://forbeslindesay.github.io/express-route-tester/

实例方法

  • setStatics({prefix, root})

    设置静态资源

  • start()

    启动服务