0.0.20-alpha • Published 9 months ago

just-rest v0.0.20-alpha

Weekly downloads
2
License
MIT
Repository
github
Last release
9 months ago

Just REST

«Just REST» is the NPM package that will help you make simple REST server.

Table of contents

Install

npm i just-rest --save

Use

1) Make module

```javascript
module.exports = {

    GET: {
       '/process-info': function(request, response){
           //http://localhost:3002/process-info
           response.resp(process.env);
       },
       
       '/process-info/([0-9]{1,})': function(request, response, matched){
           //http://localhost:3002/process-info/1234
           response.resp(matched);
       },
       
       '/process-info/error': function(request, response){
           //http://localhost:3002/process-info/error
           throw new Error('Internal Server Error');
           response.resp({});
       },
       
       '/process-info/error-401': function(request, response){
           //http://localhost:3002/process-info/error-401
           response.error(401);
       }
    },
 
    POST: {
        '/process-info': async function(request, response){
            let body = request.body;
            response.resp(body);
        }
    }, 
};
```
save module as `./modules/process-info/index.js`

2) Make interceptor

```javascript
function defineHeaders(request, response) {
    const CorsAllowHeaders = {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
        'Access-Control-Allow-Headers': 'Content-Type, Set-Cookies, Access-Token'
    };

    Object.keys(CorsAllowHeaders).forEach((item) => {
        response.setHeader(item, CorsAllowHeaders[item]);
    });
}

module.exports = {
    ANY: { //All supported methods «GET, POST, PUT, DELETE, OPTIONS»
        '(.+?)': function (request, response) {
            if (!response.finished) {
                defineHeaders(request, response);
                if (request.method === 'OPTIONS') {
                    response.statusCode = 200;
                    response.end('');
                }
            }
            return
        }
    }
};

```
save interceptor as `./interceptors/response/corsAllowHeaders.js`

2) Connect modules and interceptors to your app

```javascript
const {Modules, Server, Middlewares} = require('just-rest');

Modules.defineResponseInterceptor('./interceptors/response/corsAllowHeaders.js');
Modules.defineGlobalMiddleware(Middlewares.bodyJson); // body parser

Modules.define('./modules/process-info/index.js');

new Server({Modules, port: 3002});
```
Run app

3) Open url http://localhost:3002/process-info

Props

Props is any variable

Make props

    const {Modules, Server} = require('just-rest');
    
    Modules.define('./modules/use-props/index.js');
   
    let props = {
        test: '123456qwerty',
        date: new Date(),
        func: function(){
            console.log(`cool`)
        }
    };
    
    new Server({Modules, port: 3002, props });

Use props

make file: ./modules/use-props/index.js

module.exports = {

    GET: {
       '/use-props': function(request, response){
           //http://localhost:3002/use-props
           
           this.props.func();
           console.log(this.props.date);
           console.log(this.props.test);
           
           response.resp({});
       }
    }
};

Examples

Middleware

Use middleware in your module

const {Errors} = require('just-rest');

function user(request, response, match){
    let instance = this;

    //TODO get data from real database

    instance.user = {
        username: 'Guest',
        permissions: [],
        isAuthorized: false
    };

    let testDatabase = {
        'token1': {
            username: 'Boris',
            permissions: ['all'],
            isAuthorized: true
        },
        'token2': {
            username: 'User 2',
            permissions: ['read.me'],
            isAuthorized: true
        },
        'token3': {
            username: 'User 3',
            permissions: ['read.something'],
            isAuthorized: true
        },
    }

    if (request.headers.token && testDatabase.hasOwnProperty(request.headers.token)) {
        instance.user = testDatabase[request.headers.token];
    }

    return;
}

function isAuthorized() {
    if (!this.user.isAuthorized) {
        throw  new Errors(401)
    }
    return;
}

function readPermission() {
    if (!this.user.permissions.includes('read.me') && !this.user.permissions.includes('all')) {
        throw  new Errors(403)
    }
    return;
}

function controller(request, response, matched) {
    response.resp(this.user);
}

module.exports = {
    GET: {
        //use http://localhost:3002/profile/me
        '/profile/me': [
            user, // Using middleware. add user variable to instance
            isAuthorized, // Using middleware. check authorize
            readPermission, // Using middleware. check read permissions
            controller
        ]
    }
};

Set url path

Static url path

module.exports = {
    GET: {
        '/your-url-path-here': function (request, response) {
            //http://localhost:3002/your-url-path-here
            response.resp({success: 'ok'});
        }
    }
};

"Just Rest" supports RegExp expressions. Dynamics url path using RegExp

module.exports = {
    GET: {
        //([0-9]{1,}) = Any number 
        '/user/([0-9]{1,})': function (request, response, matched) {
            //http://localhost:3002/user/1
            let userId = matched[1];
            response.resp({userId});
        }
    }
};
0.0.20-alpha

9 months ago

0.0.19-alpha

4 years ago

0.0.18-alpha

5 years ago

0.0.17-alpha

5 years ago

0.0.16-alpha

5 years ago

0.0.15-alpha

6 years ago

0.0.14-alpha

6 years ago

0.0.13-alpha

6 years ago

0.0.12-alpha

6 years ago

0.0.11-alpha

6 years ago

0.0.10-alpha

6 years ago

0.0.9-alpha

6 years ago

0.0.8-alpha

6 years ago

0.0.7-alpha

6 years ago

0.0.6-alpha

6 years ago

0.0.5-alpha

6 years ago

0.0.4-alpha

6 years ago

0.0.3-alpha

6 years ago

0.0.2-alpha

6 years ago

0.0.1-alpha

6 years ago