This is a straightforward boilerplate for building REST APIs with ES6 and Express.
- ES6 support via babel
- REST resources as middleware via resource-router-middleware
- CORS support via cors
- Body Parsing via body-parser
- Sequelize
- MySQL
- Crypto
- HTTPS
Tip: If you are using Mongoose, you can automatically expose your Models as REST resources using restful-mongoose.
# clone it
git clone [email protected]:mguardarini/api-rest-es6-express.git
cd express-es6-rest-api
# Make it your own
rm -rf .git && git init && npm init
# Install dependencies
npm install
# Start development live-reload server
PORT=8080 npm run dev
# Start production server:
PORT=8080 npm start
Now, the most important part of all. Get an SSL certificate. A standard SSL certificate comes with two important files, Primary certificate and Intermediate certificate. To get an SSL, you will have to generate a CSR from your server for validation. Also we need to generate a key (private key) to maintain an HTTPS connection with the server. Running the following command in a linux environment will get us our desired mydomain.csr and private.key files.
openssl req -new -newkey rsa:2048 -nodes -out mydomain.csr -keyout private.key
Use this method if you already have a private key and CSR, and you want to generate a self-signed certificate with them.
This command creates a self-signed certificate (domain.crt) from an existing private key (domain.key) and (domain.csr):
Primary Certificate
`` openssl x509 -req -days 365 -in mydomain.csr -signkey private.key -out primary.crt
``
Intermediate Certificate
`` openssl x509 -req -days 365 -in mydomain.csr -signkey private.key -out intermediate.crt
``
The option(-days) specifies that the certificate will be valid for 365 days
For this example i using MySQL database, so, to test you need to create a database with the name "Test" and create one table with the name "Telemetry".
- CREATE DATABASE Test;
- use Test;
- CREATE TABLE Telemetry (hardwareId varchar(30), date dateTime, id int auto_increment primary key);
- Creating an HTTPS Server with Node.js
- Generate a Self-Signed Certificate from an Existing Private Key and CSR
- MIT