1.0.19 • Published 6 years ago

simple-men v1.0.19

Weekly downloads
1
License
ISC
Repository
-
Last release
6 years ago

Simple MEN

A bunch of wraps, middlewares, utils and configurations for ready-to-go APIs building based on MongoDB + Express + NodeJs stack.

Install

$ npm install simple-men --save

Stack

You know we're using here a MongoDB, Express and NodeJs stack. But also, we supply a Redis connection to allow your API work with cached data. Further that, we provide:

Usage

const api = require('simple-men');

api.run()
    .then(res => {
        console.log(`HTTP server-${res.enviroment} rodando em ${res.host} na porta ${res.port}.`));
    })
    .catch(err => {
        throw new Error(err);
    })

That's it! Write your express routes, controllers and mongoose models as usual and don't bother anymore with those ordinary configurations you have to deal with every time you start a new API project.

Configurations

To make possible building ready-to-go APIs, we define following the default settings:

module.exports = {
    enviroment : "dev",
    bodyParser: {
        json: {
            limit: '50mb', 
            type: 'application/json'
        }, 
        urlencoded: {
            limit: '50mb', 
            extended: true
        }
    },
    cors: [{
        route: "/*",
        header: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Headers": "Authorization, Content-Type, Origin, Accept, X-Requested-With, Origin, Cache-Control, X-File-Name",
            "Access-Control-Allow-Methods": "GET, POST, PUT, OPTIONS, DELETE"
        }
    }],
    paths : {
        schemes : [`**/scheme.js`],
        models : [`**/model.js`],
        controllers : [`**/controller.js`],
        routes : [`**/route.js`]
    },
    statics : [],
    access: {}, 
    server:{
        host : "http://localhost",
        port : 3001
    }
}

Of course you may want override some of them. Do it, but don't forget to pass your configuration as parameter to the run function such as api.run(myConfigs).then.... Also, such as every setting above, database, statics, access and redis has specific properties and by default, they will not be loaded. In case of you want define them to be used at your API project, follow the next config example using your own settings:

module.exports = {
    statics : [{
        route: '/prictures',
        path: '/public/assets/pictures'
    }],
    access: {
        admin: {
          path: "/admin/", 
          ignore: ['/favicon.ico'], 
          secret: "87jyjywwwq"
        },
        super: {
          path: "/super/", 
          ignore: ['/favicon.ico'], 
          secret: "34t34g4hjj"
        }
    }, 
    redis: {
        port: 6379,
        host: 'localhost'
    },
    database : {
        uri: "mongodb://localhost:27017/simple_men",
        debug: false
    }
}

Libs

Further than configs, Simple MEN provides wrappers to abstract some ExpressJs functions and helps to the Routes->Controller->Model->Scheme pattern flow. You're not obligated to use it, though we strongly recommend you do it.

This is how our route.js looks like:

const { router, controllers } = require('simple-men');

router
  .base("/auth")
  .post('/login', req => controllers.auth.login(req.body))
  .post('/signin', req => controllers.auth.signIn(req.body));

See the controller.js below. The only observation here is we have to export a class with static methods, because this is how we'll label it under the hoods.

const { expose, models, respond } = require('simple-men');

function signIn(data){
  const user = new models.User(data);
  
  return user.signIn()
    .then(res => respond(200).send(res))
    .catch(err => err);
}

expose.controller('auth', {
  signIn: signIn
})

Build your mongoose's models as usual, except for the use of api object. This is our model.js:

const { expose, schemes } = require('simple-men');

schemes.User.methods.signIn = () => {
  return this.save()
    .then(res => res)
    .catch(res => res);
};

expose.model('User', schemes.User);

Finnaly our scheme.js file. I use to separate my schemes from models and if you do too, remember to set a name to it when you export. Take a look:

const { expose } = require('simple-men');

expose.scheme('User', {
  name: {
    type: String,
    trim: true,
    default: ""
  },
  email: {
    type: String,
    trim: true,
    default: ""
  },
  password: {
    type: String,
    trim: true,
    default: ""
  }
});

That's all folks!

If you have any suggestion or want to contribute somehow, let me know!

License

MIT License

Copyright (c) 2017 Rodrigo Brabo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago