1.0.4 • Published 8 years ago

node-rest-annotation v1.0.4

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

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']
}
1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago