# simple-men

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

Latest version **1.0.19** (published 2017-12-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install simple-men
pnpm add simple-men
yarn add simple-men
bun add simple-men
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.19 |
| Published | 2017-12-12 |
| First published | 2017-11-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 12 |
| Known vulnerabilities | 0 (+14 in 3 direct dependencies) |
| Install scripts | no |
| Author | brab0 |
| Maintainers | brab0 |

## Links

- npm: https://www.npmjs.com/package/simple-men
- npm.io page: https://npm.io/package/simple-men

## Dependencies (12)

- [glob](https://npm.io/package/glob.md) ^5.0.13
- [path](https://npm.io/package/path.md) ^0.12.7
- [neo4j](https://npm.io/package/neo4j.md) ^2.0.0-RC2
- [config](https://npm.io/package/config.md) ^1.27.0
- [lodash](https://npm.io/package/lodash.md) ^3.10.0
- [express](https://npm.io/package/express.md) ^4.16.2
- [bluebird](https://npm.io/package/bluebird.md) ^3.5.1
- [mongoose](https://npm.io/package/mongoose.md) ^4.12.3
- [cachegoose](https://npm.io/package/cachegoose.md) ^4.0.1
- [body-parser](https://npm.io/package/body-parser.md) ^1.13.1
- [express-jwt](https://npm.io/package/express-jwt.md) ^5.1.0
- [require-files](https://npm.io/package/require-files.md) ^0.0.7

## Recent versions

- 1.0.19 (latest) — 2017-12-12
- 1.0.18 — 2017-12-12
- 1.0.17 — 2017-12-12
- 1.0.16 — 2017-12-06
- 1.0.15 — 2017-12-06
- 1.0.14 — 2017-12-06
- 1.0.13 — 2017-12-06
- 1.0.12 — 2017-12-06
- 1.0.11 — 2017-12-06
- 1.0.10 — 2017-12-06
- 1.0.9 — 2017-12-06
- 1.0.8 — 2017-12-04
- 1.0.7 — 2017-11-28
- 1.0.6 — 2017-11-28
- 1.0.5 — 2017-11-28
- … 4 more at https://npm.io/package/simple-men/versions

## README

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: 
- a `body-parser` middleware to handle your request parameters: https://www.npmjs.com/package/body-parser 
- a simple configuration for `static routes` using express.static function;
- a `JWT Middleware` to handle access controll: https://www.npmjs.com/package/express-jwt
- a `Database Connection and Schema Driven` got from Mongoose: https://www.npmjs.com/package/mongoose  
- a `Redis Connection` provided by cachegoose in case you want to work with cached data: https://www.npmjs.com/package/cachegoose
- an express `Server`: https://www.npmjs.com/package/express
- an ordinary express `cross-domain` configuration;
- and a `glob requirer` which boots your api core(routes, controllers, models and schemas): https://www.npmjs.com/package/require-files


## Usage
```javascript
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:

```javascript
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:
```javascript
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:
```javascript
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.
```javascript
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`:
```javascript
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:
```javascript
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.
```

---
_Source: https://npm.io/package/simple-men · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
