@anh4n/server
v0.4.2
Published
@anh4n/server
Downloads
2
Readme
@anh4n/server
The server package provides an already configured Node Express server.
Installation
npm install @anh4n/server
Getting Started
Controller Example
// index-controller.js
import { Controller } from '@anh4n/server';
export class IndexController {
public init(server) {
server.get('/', this.indexAction);
}
public indexAction(req, res) {
res.send('Hello World');
}
}
Controller Async Example
// index-controller.js
import { Controller } from '@anh4n/server';
import { exampleInteractor } from '@usecases/example';
export class IndexController {
public init(server) {
server.get('/', this.indexAction);
}
public async indexAction(req, res, next) {
try {
await exampleInteractor();
res.send('Hello World');
} catch (e) {
return next(e);
}
}
}
Server Start Example
// index.js
import { Server, initApp } from '@anh4n/server';
import { IndexController } from './index-controller';
initApp(() => {
const app = new Server();
app.addController(new IndexController()); // Register Controller
app.start(); // Start Server
});
initApp
This wrapper catches all errors and outputs them formatted.
Server
addController(controller: Controller)
This method can be used to register a controller on the server.
use(middleware: function)
A middleware can be registered with this method.
See: http://expressjs.com/de/api.html#app.use
start(port: number)
If no parameter is given, this method starts the server on port 3000.
getApp(): Express
Get the Express app.
getServer(): HttpServer
Get the HTTP server.