Skip to content

kuuup/node-rest-annotation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

node-rest-annotation

Create simple REST endpoints

Example

Create a server (i.e. with express)

import express from 'express';
import { bindServices } from 'node-rest-annotation';

// Class will be instantiated by this lib. 
// Constructor parameters are not available for now
import './myclass';

const app = express();

// Bind all remote methods
// If 'json' is set input parameters will parsed.
// Leave this empty and you'll get strings.
bindServices(app, 'json');

// Do more with your server
// app.use(express.static(__dirname + '/dist'));

app.listen(3000);

console.log('Listening on port 3000...'); 

Create a class that implements your remote methods

import { restApi } from 'node-rest-annotation';

export default class MyClass {

    @restApi
    method1(a) {
        return Promise.resolve([a]) ;
    }
    
    @restApi
    method2(a, b) {
        return {a: a, b: b};
    }
    
    //If you have a parameter called `req` in your annotated method it will contain the server request object.
    @restApi
    method3(req) {
        return req.headers;
    }
}

Requirements

Be sure to have your environment configured for es7. In my case I use Babel 6 and this .babelrc:

{
  'presets': ['es2015', 'stage-0'],
  'plugins': ['transform-decorators-legacy']
}

About

Create simple REST endpoints

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published