A Node.js project generator using Express and connected to MongoBD.
npm install -g nem-generator
- Authentication middleware (with JWT)
- Authorization middleware (role-based access control)
- Connection to MongoDB (using Mongoose)
- Example for User model (schema, service, controller, router)
- Config file prepared for 3 environments (dev, test, prod)
Simply run
nem-gen
and you will be prompted with questions, allowing you to configure your project (for now, the project name is the only configurable parameter during project creation).
This project follows a Model - Repository - Service - Controller
structure. This implementation is inspired by this StackOverflow answer (although it uses another terminology for the component names).
The generated project has the following files:
.
βββ app.js
βββ bin
βΒ Β βββ www
βββ config
βΒ Β βββ config.js
βΒ Β βββ config.json
βΒ Β βββ mongod.conf
βΒ Β βββ roles.js
βββ controllers
βΒ Β βββ authorizeRole.js
βΒ Β βββ usersController.js
βΒ Β βββ verifyToken.js
βββ models
βΒ Β βββ user.js
βββ package.json
βββ repositories
βΒ Β βββ userRepository.js
βββ services
βββ userService.js
In config/config.json you can configure global variables to use in your project.
Parameter | Description | Default values | Required |
---|---|---|---|
config_id | Environment name | ["development","testing","production"] for the corresponding environment |
false |
app_name | Project name | name inserted in project creation | false |
node_port | Port where the project will run | 5000 (if no value exist in the config) or [3000, 3000, 3001] for the corresponding environment |
false |
database | URL to a MongoDB database | "mongodb://127.0.0.1/{project-name}-{environment}" | true |
secret | Secret for the JWT. Make sure you change this to secure your tokens | "supersecret" | true |
tokenLife | Duration of the token expressed in seconds or a string describing a time span zeit/ms |
2678400 (31 days) | true |
Make sure you always have the required fields at least in "development", since the default project structure uses these. You can only repeat config variables in other environments if you want to override the default config variable values found in the default "development" environment.
Generated default file:
{
"development": {
"config_id": "development",
"app_name": "project-name",
"node_port": 3000,
"database": "mongodb://127.0.0.1/project-name-dev",
"secret": "supersecret",
"tokenLife": 2678400
},
"testing": {
"config_id": "testing",
"node_port": 3000,
"database": "mongodb://127.0.0.1/project-name-test"
},
"production": {
"config_id": "production",
"node_port": 3001,
"database": "mongodb://127.0.0.1/project-name-prod"
}
}
You can add your own config parameters in this file, and use them in your code with global.gConfig.{parameter-name}
, e.g. global.gConfig.database
.
Make sure verifyToken
middleware is always called before authorize
, in order to extract the role of the received token.
Here is an example:
router.get('/', verifyToken, authorize(roles.ADMIN), async function (req, res){
...
}
This function is only authorized for tokens from users with 'Admin' role.
You can also authorize multiple roles by passing an array to the authorize
, p.e.
authorize([roles.USER, roles.ADMIN])
If you don't want any role restriction, just remove the authorize
middleware.
You can also remove the token restriction, remove the verifyToken
middleware.
- Allow selection of database type (add support to SQL using Sequelize)
- Allow roles configuration during project creation
- Add optional client app template (for React, Angular or Vue)
Feel free to create issues with more ideas!
This project is licensed under the MIT License - see the LICENSE file for details