rest-coreapi-af
v1.0.56
Published
A library for build REST API in Node.js using in some personal project.
Downloads
2
Readme
CoreAPI
A library for build REST API in Node.js using in some personal project.
Installation
IMPORTANT: Requires Node.js 8 or newer.
npm install rest-coreapi-af
Quick Example
const apicore = require('rest-coreapi-af');
apicore(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;
}
//@api [default] /api/v1/hello/getmessage
getMessage() {
return { info: 'Hello World!' };
}
}
// 2. Declare and start server.
const app = core.api.startup(CGlobal => {
CGlobal.config = { server: { port: 9000 } };
});
const 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.setLogger({
// level: app.config.logLevel || 'all',
// transports: [
// new core.logging.transports.Console(),
// new core.logging.transports.File()
// ]
// });
appBuilder.onError(err => {
console.error('The app has en error!' + err.message);
});
appBuilder.onStart(server => {
console.info('server.info listen at =>' + server.address().port);
});
appBuilder.onMount(controller => {
console.info(controller + ' has mounted!');
});
appBuilder.onRoute(action => {
console.info(`method, urlPath => ${action.method} ${action.urlPath}`);
});
// this is for sample.
// appBuilder.manager.add(new HelloController());
// appBuilder.manager.register(new HelloController(), { 'getMessage': [['post', 'get'], 'message'] });
// appBuilder.setFormat((data, statuscode, error) => {
// if (error && error instanceof Error) {
// if (error.code) statuscode = error.code;
// return {
// Success: !error && !!data,
// ErrorCode: statuscode,
// Message: error.message,
// Data: ''
// };
// }
// return {
// Success: !error && !!data,
// ErrorCode: statuscode,
// Message: '',
// Data: data
// };
// });
// appBuilder.host.security.setConfig({
// cors: {
// methods: 'GET,PUT,POST,OPTIONS',
// headers: 'Content-type,Accept,X-Access-Token,X-Key'
// }
// , secret: 'example_api_test'
// , mode: core.api.security.SecurityMode.MESSAGE
// , type: core.api.security.MessageCredentialType.ISSUED_TOKEN
// , authenticate: function ({ username, password, grant_type }, callback) {
// if (!username) {
// return callback(new Error('The username must be not empty.'));
// }
// if (grant_type === 'password' && username.toLowerCase() === 'admin' && password === '1') {
// return callback(null, { username });
// }
// callback(new Error('Not Exist!'));
// }
// });
});
// server.use(middleContext => {
// // middleContext.req|res|next
//
// middleContext.accept((req, res, next) => {
// console.log('to call the middle function');
// next();
// });
// });
return server;
});