LunaticServer
- Extends
Router
.
Properties
httpServer
Type: readonly http.Server
The underlying HTTP server that receives requests and sends responses.
Methods
constructor(httpServer?: http.Server)
httpServer
:http.Server
- underlying HTTP server that receives requests and sends responses
Creates new LunaticServer
instance. You can specify underlying http.Server
, for example, to use https protocol or for integration with other frameworks, such as Socket.io:
import { LunaticServer } from '@shelepuginivan/lunatic'
import { createServer } from 'http'
import { Server } from 'socket.io'
const app = new LunaticServer()
const httpServer = createServer(app.callback)
const io = new Server(httpServer)
app.callback(req: httpIncomingMessage, res: http.ServerResponse): void
req
:http.IncomingMessage
- incoming requestres
:http.ServerResponse
- server response
Callback function that handles requests. This method can be passed in different methods and constructors, e.g. in http.Server
constructor:
import { LunaticServer } from '@shelepuginivan/lunatic'
import { Server } from 'http'
const app = new LunaticServer()
const httpServer = new Server(app.callback)
app.disable(feature: ApplicationFeature): LunaticServer
feature
:ApplicationFeature
- feature to be disabled
Disables specified app feature.
app.enable(feature: ApplicationFeature): LunaticServer
feature
:ApplicationFeature
- feature to be enabled
Enables specified app feature.
app.listen(port?: number, hostname?: string, backlog?: number): Promise<void>
port
:number | undefined
- port that the server will listen onhostname
:string | undefined
- IP address that the server will listen onbacklog
:number | undefined
- maximum length of the queue of pending connections
For more information about the arguments, see https://nodejs.org/api/net.html#serverlisten.
Starts underlying http.Server
on specified port. Essentially, this:
await app.listen(8000)
is a syntax-sugar for the following:
import { LunaticServer } from '@shelepuginivan/lunatic'
import { createServer } from 'http'
const app = new LunaticServer()
const httpServer = createServer()
httpServer.on('request', app.callback)
httpServer.listen(8000)
This allows to start different servers on different port, for example:
import { createServer as createHttpServer } from 'http'
import { createServer as createHttpsServer } from 'https'
const app = new LunaticServer()
const httpServer = createHttpServer(app.callback)
const httpsServer = createHttpsServer(app.callback)
httpServer.listen(80)
httpsServer.listen(443)
app.renderer(renderFunction: RenderFunction): LunaticServer
renderFunction
:RenderFunction
- function that is used to render HTML templates
Specifies RenderFunction
that will be used in Response.render()
and Response.renderFile()
for rendering HTML templates.
app.toggle(feature: ApplicationFeature): LunaticServer
feature
:ApplicationFeature
- feature to be toggled
Toggles specified app feature.