Skip to content

Node.js express.js MySQL REST API - This is a basic API REST skeleton written on JavaScript. Great for building a starter web API for your front-end

License

Notifications You must be signed in to change notification settings

19jake68/node-express-mysql-rest-api-skeleton

Repository files navigation

Node.js express.js MySQL REST API - Basic Project Skeleton

Getting started

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.

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 value Bearer yourToken where yourToken is the signed and encrypted token given in the response from the login process.

Requirements

  • Node.js 10+

How to install

Using Git (recommended)

  1. 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

Using manual download ZIP

  1. Download repository
  2. Uncompress to your desired directory

Install npm dependencies after installing (Git or manual download)

cd myproject
npm install
npm update

Setting up environments (development or production)

  1. In the root this repository you will find a file named .env.example
  2. Create a new file by copying and pasting the file and then renaming it to just .env
  3. The file .env is already ignored, so you never commit your credentials.
  4. Change the values of the file to your environment (development or production)
  5. Upload the .env to your environment server(development or production)
  6. 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

i18n

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.

How to run

Running in development mode (lifting API server)

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
****************************

Running tests

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

Formatting code

Format your code with prettier by typing:

npm run format

Formatting markdown files

Format all your markdown files with remark by typing:

npm run remark

Linting code

Lint your code with ESLint by typing:

npm run lint

Creating new models

If you need to add more models to the project just create a new file in /app/models/ and it will be loaded dynamically.

Creating new routes

If you need to add more routes to the project just create a new file in /app/routes/ and it will be loaded dynamically.

Creating new controllers

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.

Bugs or improvements

Feel free to report any bugs or improvements. Pull requests are always welcome.

License

This project is open-sourced software licensed under the MIT License. See the LICENSE file for more information.

About

Node.js express.js MySQL REST API - This is a basic API REST skeleton written on JavaScript. Great for building a starter web API for your front-end

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published