apicorev3
v1.0.33
Published
A library for build REST API in Node.js using in some personal project.
Downloads
8
Readme
CoreAPI
A library for build REST API in Node.js using in some personal project.
Installation
IMPORTANT: Requires Node.js 6 or newer.
npm install apicorev3
Quick Example
const apicorev3 = require('apicorev3');
apicorev3(core => {
// 1. Declare some controller with public API.
class HelloController extends core.api.Controller {
constructor() {
super();
this.namespace('api/v1', () => {
this.resources('hello'); // => /api/v1/hello
});
// this.allowAnonymous = true;
// this.isHidden = true;
}
//@api [default] /api/v1/hello/getmessage
getMessage() {
return { info: 'Hello World!' };
}
}
// 2. Declare and start server.
var app = core.api.startup(CGlobal => {
CGlobal.config = { server: { port: 9000 } };
});
var server = app.newServer({ port: app.config.server.port }, (err, info) => {
if (err) {
console.log('server.err =>' + err.message);
} else {
console.log('server.info =>' + info.address().port);
}
});
server.register(appBuilder => {
appBuilder.onLoad(() => {
console.log('The app has load!!!');
});
appBuilder.onMount((controller) => {
console.log(controller + ' has mounted!');
});
appBuilder.onRoute((handler) => {
console.log(`method, urlPath => ${handler.method} ${handler.urlPath}`);
});
// normaly you only must call.
// appBuilder.load();
// this is for sample.
appBuilder.manager.add(new HelloController());
// appBuilder.manager.register(new HelloController(), { 'getMessage': [['post', 'get'], 'message'] });
});
// server.use(middleContext => {
// // middleContext.req|res|next
//
// middleContext.accept((req, res, next) => {
// console.log('> call to the middle function');
// next();
// });
// });
return server;
});