1.2.1 • Published 6 years ago

micserver v1.2.1

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

micserver.js

It can bing routers to handlers and start a native nodejs service very quickly

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

start service in http

const micserver = require('micserver')

const ms = micserver('http')
ms.Handle({
    path: '/',
    method: 'GET',
    func: (req, res) => {
        res.write('Hello World')
        res.end()
    }
})

ms.listen()

Start an http service and https service at the same time

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

micserver(options)

Only support http or https type, return http or https server

type: http

.set({options})

PropertyDescriptionTypeDefault
hostThe IP address of the service listenerfor example 127.0.0.1 or 0.0.0.0String"0.0.0.0"
portService listening the portNumber80

.Handle({options})

PropertyDescriptionTypeDefault
methodMethod of requesting serviceString'GET'
pathPath of requesting serviceString'/'
funcService handler. This handler takes two parameters: request and response.Function

.listen()

Start the service with the default or option configured in the set

type: https

.set({options})

PropertyDescriptionTypeDefault
keypathThis field is required if the type is https.Need to configure the path of the key and certObject{key: '',cert: ''}
hostThe IP address of the service listenerfor example 127.0.0.1 or 0.0.0.0String"0.0.0.0"
portService listening the portNumber443

.Handle({options})

PropertyDescriptionTypeDefault
methodMethod of requesting serviceString'GET'
pathPath of requesting serviceString'/'
funcService handler. This handler takes two parameters: request and response.Function

.listen()

Start the service with the default or option configured in the set

1.2.1

6 years ago

1.2.0

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago