A HTTP server itself is a HTTP request listener, which can be used in any compatible server instance, like Node HTTP, e.g.
import createServer from '@egomobile/http-server'
import { createServer as createHTTPServer } from 'http'
const app = createServer()
const server = createHTTPServer(app)
server.listen(8080, () => {
console.log('Server is listing')
})
Readonly
errorGets the current error handler.
Readonly
instanceThe underlying low-level server instance, which is only available
if server is listening, otherwise it is null
.
Mostly this is a Server
instance from http
module.
Readonly
isIndicates if that instance is an e.GO HTTP server.
import assert from 'assert'
import createServer from '@egomobile/http-server'
const app = createServer()
assert.strictEqual(app.isEgoHttpServer, true)
Readonly
notGets the current "not found" handler.
Optional
Readonly
portThe current TCP port or (undefined) if server is not running.
Registers a route for all possible request methods.
The path.
The handler.
import createServer, { IHttpRequest, IHttpResponse } from '@egomobile/http-server'
const app = createServer()
app.all('/', async (request: IHttpRequest, response: IHttpResponse) => {
// do your magic here
})
await app.listen() // port === 8080, if NODE_ENV === 'development'; otherwise 80
Registers a route for a CONNECT request.
The path.
The handler.
import createServer, { IHttpRequest, IHttpResponse } from '@egomobile/http-server'
const app = createServer()
app.connect('/', async (request: IHttpRequest, response: IHttpResponse) => {
// do your magic here
})
await app.listen() // port === 8080, if NODE_ENV === 'development'; otherwise 80
Loads an initializes the controllers from and inside a root directory.
The result of the invocation with call data of the setup.
import createServer from '@egomobile/http-server'
const app = createServer()
// scans the subdirectory 'controllers' of the current process'
// working directory for JavaScript and/or TypeScript files,
// which do not start with _ and have a class as default export,
// that is marked with @Controller decorator
// and creates instances from that classes, which will be automatically
// mapped as handlers for 'app' instance
app.controllers()
await app.listen()
Optional
imports: Nilable<ImportValues>Registers a route for a DELETE request.
The path.
The handler.
import createServer, { IHttpRequest, IHttpResponse } from '@egomobile/http-server'
const app = createServer()
app.delete('/', async (request: IHttpRequest, response: IHttpResponse) => {
// do your magic here
})
await app.listen() // port === 8080, if NODE_ENV === 'development'; otherwise 80
Emits an event.
The name of the event to emit.
The result.
Registers a route for a GET request.
The path.
The handler.
import createServer, { IHttpRequest, IHttpResponse } from '@egomobile/http-server'
const app = createServer()
app.get('/', async (request: IHttpRequest, response: IHttpResponse) => {
// do your magic here
})
await app.listen() // port === 8080, if NODE_ENV === 'development'; otherwise 80
Registers a route for a HEAD request.
The path.
The handler.
import createServer, { IHttpRequest, IHttpResponse } from '@egomobile/http-server'
const app = createServer()
app.head('/', async (request: IHttpRequest, response: IHttpResponse) => {
// do your magic here
})
await app.listen() // port === 8080, if NODE_ENV === 'development'; otherwise 80
Starts listening for connections.
Optional
port: Nilable<string | number>The custom TCP port. Default is 8080 in development mode (NODE_ENV), otherwise 80.
import createServer from '@egomobile/http-server'
const app = createServer()
await app.listen() // if NODE_ENV === 'development', port is 8080
// otherwise port is 80
await app.listen(5979) // explicit port 5979
Registers an event handler.
The name of the event.
The handler to execute.
Registers a route for a OPTIONS request.
The path.
The handler.
import createServer, { IHttpRequest, IHttpResponse } from '@egomobile/http-server'
const app = createServer()
app.options('/', async (request: IHttpRequest, response: IHttpResponse) => {
// do your magic here
})
await app.listen() // port === 8080, if NODE_ENV === 'development'; otherwise 80
Registers a route for a PATCH request.
The path.
The handler.
import createServer, { IHttpRequest, IHttpResponse } from '@egomobile/http-server'
const app = createServer()
app.patch('/', async (request: IHttpRequest, response: IHttpResponse) => {
// do your magic here
})
await app.listen() // port === 8080, if NODE_ENV === 'development'; otherwise 80
Registers a route for a POST request.
The path.
The handler.
import createServer, { IHttpRequest, IHttpResponse } from '@egomobile/http-server'
const app = createServer()
app.post('/', async (request: IHttpRequest, response: IHttpResponse) => {
// do your magic here
})
await app.listen() // port === 8080, if NODE_ENV === 'development'; otherwise 80
Registers a route for a PUT request.
The path.
The handler.
import createServer, { IHttpRequest, IHttpResponse } from '@egomobile/http-server'
const app = createServer()
app.put('/', async (request: IHttpRequest, response: IHttpResponse) => {
// do your magic here
})
await app.listen() // port === 8080, if NODE_ENV === 'development'; otherwise 80
Sets a custom error handler.
The new handler.
import createServer, { IHttpRequest, IHttpResponse } from '@egomobile/http-server'
const app = createServer()
app.setErrorHandler(async (error: any, request: IHttpRequest, response: IHttpResponse) => {
const errorMessage = Buffer.from('SERVER ERROR: ' + String(error))
if (!response.headersSent) {
response.writeHead(500, {
'Content-Length': String(errorMessage.length)
})
}
response.write(errorMessage)
response.end()
})
app.get('/', async (request: IHttpRequest, response: IHttpResponse) => {
throw new Error('Something went wrong')
})
await app.listen()
Sets a new 'not found' handler.
The new handler.
import createServer, { IHttpRequest, IHttpResponse } from '@egomobile/http-server'
const app = createServer()
app.setNotFoundHandler(async (request: IHttpRequest, response: IHttpResponse) => {
const errorMessage = Buffer.from(`The page ${request.url} could not be found`)
if (!response.headersSent) {
response.writeHead(404, {
'Content-Length': String(errorMessage.length)
})
}
response.write(errorMessage)
response.end()
})
// ... your routes
await app.listen()
Runs tests.
Optional
options: Nilable<IHttpServerTestOptions>Custom options.
The promise with the result.
Registers a route for a TRACE request.
The path.
The handler.
import createServer, { IHttpRequest, IHttpResponse } from '@egomobile/http-server'
const app = createServer()
app.trace('/', async (request: IHttpRequest, response: IHttpResponse) => {
// do your magic here
})
await app.listen() // port === 8080, if NODE_ENV === 'development'; otherwise 80
Adds one or more global middlewares.
Optional
Rest
...middlewares: HttpMiddleware[]The middlewares to add.
import assert from 'assert'
import createServer from '@egomobile/http-server'
const app = createServer()
assert.strictEqual(app.isEgoHttpServer, true)
app.use(async (request: any, response: any, next) => {
request.foo = '1'
next()
}, async (request: any, response: any, next) => {
// foo is currently '1'
request.foo += 2
next()
})
app.get('/', async (request: any, response: any) => {
response.write(String(request.foo === '12'))
})
Generated using TypeDoc
A HTTP server instance.