0.0.15 • Published 3 months ago

fe-dev-server-core v0.0.15

Weekly downloads
-
License
ISC
Repository
-
Last release
3 months ago

fe-dev-server-core

A local server for frontend development, providing function like request proxy, API mock, static assets service and more

Installation

mkdir your-server-workspace
cd your-server-workspace

npm init -y
npm install fe-dev-server-cli fe-dev-server-core --save

# install plugins in need
npm install fe-dev-server-plugin-proxy

# create config file in current directory, using -c to specify filename, fe-dev-server.config.js as default
./node_modules/.bin/fe-dev-server init

# modify the generated config file, then run serve to start the server
# the server will restart automatically when config file change
./node_modules/.bin/fe-dev-server serve

fe-dev-server.config.js

const { ProxyPlguin } = require('fe-dev-server-plugin-proxy');

/**
 * @type {import("fe-dev-server-core").FeDevServerCfg}
 */
const config = {
  port: 8080,
  host: 'localhost',
  https: false,
  logger: { level: 'info' },
  plugins: [
    // register the plugin in need, proxy for example
    new ProxyPlguin({
      cfg: {
        target: 'https://www.google.com',
      },
    }),
  ],
};

module.exports = config;

Config

host

string, Default: 'localhost' - serving host

port

number, Default: random available port - serving port

https

false / https.ServerOptions, Default: false - serving ssl config

logger

false / object, Default: false (alias for slient) - logger options

logger.level: string - slient / trace / debug / info /warn / error / fatal

realTimeLog

real time log is a friendly display mode for key infomation, usually used by user

operation

object - operation can create additional server, which can provide APIs for external invocation through the specified methods defined in the plugin

operation.enable: boolean, Default: false - weather to enable operation server

operation.host: string, Default: 'localhost' - serving host

operation.port: number, Default: random available port - serving port

operation.https: false / https.ServerOptions, Default: false - serving ssl config

conflict with logger(console), should only choose one from logger(console) or real time log

false / object, Default: see below - real time log options

realTimeLog.enable: boolean, Default: true - wheather to show real time log

realTimeLog.interval: number, Default: 100 - refresh time for real time log

addReplyHeader

boolean, Default: false - wheather to add response header marking the plugin make response

http2Wrapper

object - http2Wrapper will create an additional server, which can provide an http2 request handler before fe-dev-server

http2Wrapper.enable: boolean, Default: false - weather to enable wrapper server

http2Wrapper.host: string, Default: 'localhost' - serving host

http2Wrapper.port: number, Default: random available port - serving port

http2Wrapper.serverOptions: http2.SecureServerOptions, - serving http2 config

If you are developing plugin with hijack call, you should implement this manually

plugins

array of FeDevServerPlugin - serving logic depends on the plugins you create

plugin has some common config:

  • enable: true / false, Default: true - activation of the plugin
  • cfg: object, Default: undefined - config of the plugin
  • match: string / RegExp / function, Default: '/' - activation of the plugin according to the request
  • macth: string - using pathname prefix to devide the activation of the plugin, for examples, '/api/v1/login' is active while '/api/v2/login' is inactive when match is '/api/v1'
  • match: RegExp - using pathname to check with the regexp, for exampels, '/api/v1/login' is active while '/api/v2/login' is inactive when match is /v1/
  • match: function - pathname / method / search / headers passed to function, write your custom logic to decide the activation
  • throwNotFoundWhenUnreplied: boolean, Default: false - throw not found when this plugin's handler executed without response
  • uuid: string, Default: random uuID string - a unique string for distinguish plugin
  • operation: object - config of operation in plugin
  • operation.enable: boolean - weather to enable operation methods in plugin
  • operation.urlPrefix: string - the url prefix for accessing the API

Built-in plugins