@nedvisol/hexagon-adapter-express
v0.1.1
Published
ExpressJS adapter for building portable NodeJS microservice
Downloads
2
Readme
Overview
This package provides an adapter that maps
The motivation behind this library is to help make your Node microservice code more portable, between web frameworks (such as Express or Koa) or Serverless model with cloud functions. We believe in the Hexagonal architecture where your core business logic should be loosely-coupled with the presentation interface and the underlying services.
Example
Install Hexagon via NPM
$ npm install @nedvisol/hexagon-core @nedvisol/hexagon-adapter-express
Write your first controller by creating a plain-old Typescript class and annotate
// index.ts
import { RestController, RestGetMapping, RestPathParam } from '@nedvisol/hexagon-core';
@RestController()
export class UserController {
@RestGetMapping('/users/:id')
getUser (@RestPathParam('id') id: string): IUser {
return { id, name: 'test' };
}
}
Then apply to your favorite web/API server
// index.ts (cont..)
import { initializeExpressApp } from '@nedvisol/hexagon-adapter-express';
const app = express();
app.use(bodyParser.text());
initializeExpressApp(app);
app.listen(3000, () => {
console.log('listening on port 3000!');
});
Start the server
$ ts-node index.ts
In a different terminal
$ curl -v http://localhost:3000/users/id-001
You should see a response:
{"id": "id-001", "name": "test"}