This is a basic API REST skeleton written on JavaScript using async/await. Great for building a starter web API for your front-end
This project is created to help other developers create a basic REST API in an easy way with Node.js. This basic example shows how powerful and simple JavaScript can be. Do you want to contribute? Pull requests are always welcome to show more features.
- Multiple environment ready (development, production)
- Compressed responses.
- Secured HTTP headers.
- CORS ready.
- HTTP request logger in development mode.
- i18n ready.
- Login access log with IP, browser and country location (for country it looks for the header
cf-ipcountry
that CloudFlare creates when protecting your website). - API autogenerated documentation by Postman.
- Testing with mocha/chai for API endpoints.
- NPM script for keeping good source code formatting using prettier and ESLint. (AirBnb Style Guide)
- Use of ESLint for good coding practices.
- Ability to refresh token
- JWT Tokens, make requests with a token after login with
Authorization
header with valueBearer yourToken
whereyourToken
is the signed and encrypted token given in the response from the login process.
- Node.js 10+
- Clone the project from github. Change "myproject" to your project name.
git clone https://github.com/19jake68/node-express-mysql-rest-api-skeleton.git ./myproject
- Download repository
- Uncompress to your desired directory
cd myproject
npm install
npm update
- In the root this repository you will find a file named
.env.example
- Create a new file by copying and pasting the file and then renaming it to just
.env
- The file
.env
is already ignored, so you never commit your credentials. - Change the values of the file to your environment (development or production)
- Upload the
.env
to your environment server(development or production) - If you use the postman collection to try the endpoints, change value of the variable
server
on your environment to the url of your server, for development mode use http://localhost:3000
Language is automatically detected from Accept-Language
header on the request. So either you send locale manually on the request or your browser will send its default, if Accept-Language
header is not sent then it will use en
locale as default.
npm run dev
You will know server is running by checking the output of the command npm run dev
****************************
* Starting Server
* Port: 3000
* APP_ENV: development
* Database: MySQL
* DB Connection: OK
****************************
It´s a good practice to do tests at your code, so a sample of how to do that in mocha/chai
is also included in the /test
directory
npm run test
Format your code with prettier by typing:
npm run format
Format all your markdown files with remark by typing:
npm run remark
Lint your code with ESLint by typing:
npm run lint
If you need to add more models to the project just create a new file in /app/models/
and it will be loaded dynamically.
If you need to add more routes to the project just create a new file in /app/routes/
and it will be loaded dynamically.
When you create a new controller file, try to also create another file with validations. Ex. countries.js
and countries.validate.js
. An example of this is included in the repository.
Feel free to report any bugs or improvements. Pull requests are always welcome.
This project is open-sourced software licensed under the MIT License. See the LICENSE file for more information.