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

neg-express-server

v0.2.0

Published

Newegg express server.

Downloads

8

Readme

neg-express-server

结合常用套路,利用Express搭建的一套NODE服务端开发库,能够简化项目的初始化。

如何用?

# 安装
npm i -S neg-express-server

# 常规使用
const negExpressServer = require('neg-express-server');
const config = require('./config');

negExpressServer.initNegConfig({ env: 'gdev', system: 'newkit', key: 'newkit-plugin-registry-config' })
  .then(conf => {
    config.setConfig(conf);
    return conf;
  })
  .then(conf => {
    return negExpressServer.startServer({
      port: conf.port,
      routesPath: path.join(__dirname, 'routes')
    });
  })
  .then(server => {
    let addr = server.address();
    console.log(`Server started at ${addr.address}:${addr.port}...`);
  })
  .catch(console.error);


# config.js内容
const config = {
  setConfig(conf) {
    // 防止覆盖setConfig
    if (conf.setConfig) {
      console.warn('config key setConfig not allowed.');
      delete conf.setConfig;
    }
    Object.assign(this, conf);
  }
};
module.exports = config;

如果不使用 Config Service,那么用法更简单,如下:

const config = require('./config'); 
negExpressServer.startServer({
  port: config.port,
  routesPath: path.join(__dirname, 'routes')
})
  .then(server => {
    let addr = server.address();
    console.log(`Server started at ${addr.address}:${addr.port}...`);
  })
  .catch(console.error);

配置项

整个库涉及到有两个配置项,一个是初始化NegConfig时,需要提供一个配置项:

initNegConfig(options)

其中 options 包含以下属性:

  1. env { string } -- 配置运行环境,可选 gdev, gqc, prdtesing, prd,表示从哪个环境读取配置
  2. connectionString { string } -- 和env任选其一(必选其一),表示从哪个地址读取config,会优先读取connectionString。
  3. system { string } -- 必选,ConfigService中的system。
  4. key { string } --必填,ConfigService中的key

注意,在配置config service的时候,请设置config 格式为JSON。

另外一个配置项是启动Express Server时候的配置项,如下:

negExpressServer.startServer(options);

其中 options 包含以下属性:

1. port { number } 服务启动端口,默认3000
2. enableApiErrorHandler { bool } 是否启用API错误处理,默认true
3. enableCors { bool } 是否启用cors,默认true
4. corsOptions { object } CORS相关设置,请参考package cors
5. enableResponseTime { bool } 是否启用响应时间输出,默认true
6. responseTimeOptions: {}, 响应时间输出配置,请参考package response-time
7. enableMorganLog: { bool } 是否启用morgan日志,默认true
8. logFormat { string } Morgan日志格式,默认 'combined'
9. logOptions { object } Morgan日志配置对象,请参考package morgan
10. enableFaq: { bool } 是否启用FAQ,默认true
11. faqOptions: { object } // FAQ请求参数
    1. path { string } faq path。默认 /faq
    2. result { any } faq请求返回值,默认 'ok'
12. urlEncodedOptins: { // URL转换参数
    extended: false,
    limit: '1mb'
  },
13. jsonParserOptions: { // JSON转换参数
    limit: '1mb'
  },
14. routesPath: path.join(process.cwd(), 'routes') 路由所在的目录配置,支持多级目录