Web server based on nodejs

NODE-WEB-SERVER

WEB SERVER written by nodejs

function list

  • Support static files
  • Support script file
  • Support extension

Quick start

Example 1:

Automatic port setting

let webserver = new WebServer()
console.log(`Web server port is ${webserver.port}.`)

Example two:

Set port

let settings: Settings = { port: 8085 }
let webserver = new WebServer(settings)
console.log(`Web server port is ${webserver.port}.`)

Example three:

Set up website folder

let settings: Settings = { websiteDirectory: 'your website path' }
let webserver = new WebServer(settings)
console.log(`Web server port is ${webserver.port}.`)

Example four:

Use dynamic script files

  • Create website folder
  • Create a cgi-bin folder in the website folder
  • Create the hello-world.js file

The folders are as follows:

website
|--cgi-bin
|--|--hello-world.js
|--index.js

Contents of index.js file:

const { WebServer } = require('maishu-node-web-server')
let webserver = new WebServer({
  port: 8080,
  websiteDirectory: 'your website path'
})

The content of the hello-world.js file:

exports.default = function (args) {
  content: 'Hello World'
}

Enter http://127.0.0.1:8080/hello-world.js in the browser address bar , the browser displays the content:

Hello World

node-web-server settings

Set up

export  interface  Settings  { 
  /** Service port*/ 
  port ?: number 
  /** The bound IP address, the client can only connect through the bound IP, blank means all available IP */ 
  bindIP ?: string 
  /* * Log*/ 
  log ?: { 
    /** Log level*/ 
    level ?: LogLevel 
  } 
  /** Request processor type*/ 
  requestProcessorTypes ?: {  new  ( config ?: any ) : RequestProcessor  } [ ] 
  /** Request processing Configuration*/ 
  requestProcessorConfigs ?: {  [ key : string] : any  } 
  /** Website Directory*/ 
  websiteDirectory ?: string | VirtualDirectory 
  /** 
  RequestResultTransforms */ requestResultTransforms ?: RequestResultTransform [ ] 
}

Example

Request handler

node-web-server processes the requests submitted by the client through the RequestProcessor class. Set by the requestProcessorTypes (array type) parameter of settings. Node-web-server has three built-in request processors, namely ProxyRequestProcessor, CGIRequestProcessor, and StaticFileRequestProcessor. They handle three types of requests:

  • Request agent
  • Dynamic script request
  • Static file request

Request agent

The ProxyRequestProcessor class is used to process proxy requests, and it is built into node-web-server. Setting definition:

export interface ProxyItem {
  targetUrl: string
  headers?:
    | { [name: string]: string }
    | ((
        requestContext: RequestContext
      ) => { [name: string]: string } | Promise<{ [name: string]: string }>)
}

export interface ProxyConfig {
  proxyTargets: { [key: string]: ProxyItem | string }
}

ProxyRequestProcessor is built-in in node-web-server, it only needs configuration to use.

Example

let  proxyConfig : ProxyConfig  =  { 
  proxyTargets : { 
    '/AdminWeiXin/(\\S+)' : { 
      targetUrl : `http://127.0.0.1: ${ station . port } /$1` , 
      headers : function  ( )  { 
        return  { token } 
      } 
    } , 
    // Used to test asynchronous headers 
    '/Proxy1/(\\S+)' : { 
      targetUrl : `http://127.0.0.1: ${ station . port }/$1`,
      headers: async function () {
        return { token }
      }
    }
  }
}

let w = new WebServer({
  requestProcessorConfigs: {
    Proxy: proxyConfig
  }
})

Request handling mechanism

Settings interface definition

export interface Settings {
  port?: number
  bindIP?: string
  logLevel?: LogLevel
  requestProcessorTypes?: { new (config?: any): RequestProcessor }[]
  requestProcessorConfigs?: { [key: string]: any }
  websiteDirectory?: string | VirtualDirectory
  requestResultTransforms?: RequestResultTransform[]
}

RequestProcessor interface definition

export interface RequestProcessor {
  execute(
    args: RequestContext
  ): RequestResult | Promise<RequestResult | null> | null
}

The node-web-server creates a set of RequestProcessor objects according to the requestProcessorTypes array, and processes the requests submitted by the client through this RequestProcessor array (calls the execute method). If the execute method returns a null value, it calls the execute method of the next RequestProcessor object. If the returned value is not a null value, the result is output to the client.

Add request processing

Download Details:

Author: ansiboy

Source Code: https://github.com/ansiboy/node-web-server

#nodejs #node #javascript

Web server based on nodejs
2.65 GEEK