mjrdnk / express-simple-mvc

Simple starter, bare bone structure based on ExpressJS. Helps easily creating controllers for routes. Provides Flightplan file for simple deployment strategy.
MIT License
12 stars 3 forks source link

express-simple

A project for fast development of REST APIs, where routes can return JSON documents

Simple starter, barebone structure based on ExpressJS. Helps to easily create controllers for routes. Provides Flightplan file for simple deployment strategy.

This project uses the following libraries:

Installation

Open your terminal and run the following commands:

git clone https://github.com/mjrdnk/express-simple
cd express-simple/ && npm install

Usage

To run express-simple in development, run this command from your project directory:

npm run dev

To run express-simple in production, run this command from your project directory:

npm start

Then, go to http://localhost:5000/api/v1.0/hello

You will see this screen in your browser


Examples

Example : nexmo sms service

To write your custom controllers write your functions in a file and export the module, like this:

const Nexmo = require('nexmo');

function nexmo () {
    this.nexmo = {}
    this.apiKey = yourapiKey; // get your api key and secret from nexmo.com
    this.apiSecret = yourapiSecret;
    this.sendSms = sendSms;
}

this.nex = new Nexmo ({
    apiKey : this.apiKey,
    apiSecret : this.apiSecret
});

let sendSms = (from,to,message) => {
            this.nex.message.sendSms(from, to, message, (err,responseData) => {
        if (err) {
            console.log('err', err.message);
            return;
        }
    });
}
module.exports = nexmo;

The controller can be reused in a router nexmo.route.js file, like this:

const router = require(express).Router();

const NexmoController = require('../controllers/nexmo.js');

router.get('/sms', NexmoController.sendSms);

module.export = router

Then you can use it in your server.js file, like this:

const nexmo = require('./routes/nexmo.route.js');

app.use(API_BASE, nexmo);

Enviroment Variables

$SSH_AUTH_SOCK

Contains the path of the unix file socket that the agent uses for communication with other processes. This is essential for ssh-add. See more details.

Credit

Flightplan based on this gist

License

MIT