Multiplexing in Nodejs, from Golang.Can quickly open a simple server based on the path
const micserver = require('micserver')
const ms = micserver('http')
ms.Handle({
path: '/',
method: 'GET',
func: (req, res) => {
res.write('Hello World')
res.end()
}
})
ms.listen()
`
Then you visit http://localhost:80 and you can receive the following message
`
Hello World
`
Installation
`npm install micserver`
Examples
$3
`
const micserver = require('micserver')
const ms = micserver('http')
ms.Handle({
path: '/',
method: 'GET',
func: (req, res) => {
res.write('Hello World')
res.end()
}
})
ms.listen()
`
$3
`
const micserver = require('micserver')
const ms1 = micserver('http')
const ms2 = micserver('https')
ms1.Handle({
path: '/',
method: 'GET',
func: (req, res) => {
res.write('Hello World')
res.end()
}
})
ms2.set({
port: 443,
keypath: {
key: './key',
cert: './cert'
}
})
ms2.Handle({
path: '/',
method: 'GET',
func: (req, res) => {
res.write('Hello World')
res.end()
}
})
ms1.listen()
ms2.listen()
`
Then http server and https server will listen to ports 80 and 443 respectively.
Api
$3
Only support http or https type, return http or https server
$3
#### .set({options})
|Property|Description|Type|Default|
|:-|:-|:-|:-|
|host|The IP address of the service listener
for example 127.0.0.1 or 0.0.0.0|String|"0.0.0.0"|
|port|Service listening the port|Number|80|
#### .Handle({options})
|Property|Description|Type|Default|
|:-|:-|:-|:-|
|method|Method of requesting service|String|'GET'|
|path|Path of requesting service|String|'/'|
|func|Service handler. This handler takes two parameters:
request and response.|Function||
#### .listen()
Start the service with the default or option configured in the set
$3
#### .set({options})
|Property|Description|Type|Default|
|:-|:-|:-|:-|
|keypath|This field is required if the type is https.
Need to configure the path of the key and cert|Object|{key: '',cert: ''}|
|host|The IP address of the service listener
for example 127.0.0.1 or 0.0.0.0|String|"0.0.0.0"|
|port|Service listening the port|Number|443|
#### .Handle({options})
|Property|Description|Type|Default|
|:-|:-|:-|:-|
|method|Method of requesting service|String|'GET'|
|path|Path of requesting service|String|'/'|
|func|Service handler. This handler takes two parameters:
request and response`.|Function||