node-mysql-rest-api
node-mysql-rest-api copied to clipboard
👌🏻 A boilerplate application for building RESTful APIs Microservice in Node.js using Express and Sequelize-MySQL in ES6 with JsonWebToken Authentication and Node-Casbin authorization access control.
node-mysql-rest-api
Simple Node.js Boilerplate for simple Node.js Applications. Inspired by septa97's node-boilerplate, Kunal Kapadia's express-mongoose-es6-rest-api, and brianschardt's node_rest_api_mysql
A boilerplate for building applications in Node.js using ES6 with Code Coverage. Follows Airbnb's Javascript Style Guide.
Features
| Feature |
|---|
| ES6 using Babel |
| Run tests using Mocha |
| Code linting using ESLint |
| Automatic syntax formatting using prettier |
| Auto-restart server using nodemon |
| Logging using debug |
| HTTP access control using cors |
| Authorization access control using node-casbin |
| API parameter validation using express-validation |
| Code coverage using istanbul |
| HTTP status code and message http-status |
| Consistent commit syntax using commitizen and AngularJS's commit message convention |
| Precommit hook by running the linter and code coverage tool |
| Authentication using Passport.js and JSON Web Tokens |
| Password hashing using bcryptjs |
Installing / Getting started
Clone the repository and name it as you like.
git clone https://github.com/JoemaNequinto/node-mysql-rest-api.git <your-project-name>
cd your-project-name/
rm -rf .git/ && git init
git remote add origin https://github.com/<USERNAME>/<REPOSITORY>.git
npm run --silent create
cp .env.example .env
rm README.md
mv README.sample.md README.md
npm install
Then fill up the prompt for modifying the package.json.
You must delete the .git folder and re-initialize it using git init.
You must also create a new README.md. Open the new README.md then modify it depending on your project.
The above code installs the dependencies, creates an environment file, and modifies the package.json file.
MySQL create database
Simple script to create MySQL database and user.
cd scripts/
sudo chmod 755 mysql-db-create.sh
sudo ./mysql-db-create.sh <dbname> <dbuser> <password>
You must enter the password of MySQL root user.
You must change the values of DB_NAME, DB_USER, DB_PASSWORD in .env with dbname, dbuser, password
The above code creates a MySQL database, user and password; modifies the .env file.
Developing the application
To start developing with code linter,
npm run dev
Add an access to a route for a certain role
Edit the policy.csv file in src/lib/acl/
Add an entry:
1st column - p
2nd column - sub (role eg. admin)
3rd column - obj (resource or route eg. /users)
4th column - act (method eg. POST, GET, PUT, DELETE)
For more info, read the casbin documentation.
Making a commit
To safely follow the standards of a commit,
npm run cm
then follow the instructions.
Tests
To run tests,
npm run test