0.4.1 • Published 7 years ago
npts v0.4.1
npts
A minimal, modular TypeScript framework for creating and reusing NodeJS REST API Servers.
Usage
Creating a server
// src/server.ts
import * as npts from 'npts';
import AppModule from './modules/app-module';
const server = new npts.Server();
server.load({
dbConnector: npts.Databases.MONGO,
modules: [ AppModule ],
port: 3000,
});
Creating a server automatically starts that server once all modules
have been fully loaded.
Creating a Module
// src/modules/app-module/index.ts
import { AbstractModel, BaseModule, RouteInfo } from 'npts';
import * as express from "express";
import UserModel from './models/test-model';
export default class AppModule extends BaseModule {
constructor() {
super();
this.addRoute(RouteInfo.RouteType.GET, '/', [this.index]);
}
public get Models(): Array<typeof AbstractModel> {
return [ UserModel ];
}
public index(req: express.Request, res: express.Response, next: express.NextFunction) {
res.send('hello world');
}
}
Navigating to localhost:3000/
will now show 'hello world' in your browser!
Models
// src/modules/app-module/models/user-model.ts
import { AbstractModel } from 'npts';
import { auto, registerFields, uniq } from 'npts/metadata';
@registerFields(
'id', 'name',
)
class TestModel extends AbstractModel {
@uniq(true)
@auto(true)
public id: number;
@uniq(true)
get name(): string {
return this._name;
}
private _name: string = 'test-model';
constructor() {
super();
this.id = 1;
}
}