Post 02 in a series of posts about writing an Express REST API using Sequelize object rational mapping for SQL databases.
In this post we will steps through setting up the express server within the project
Install NPM Dependencies
Lets start by install the express module and dotenv.
Dotnev is helpful because we can set our environmental variables to use during development in a .env file and add it to the .gitignore to avoid sharing secret information with the internet.
Create Folder / File Structure
Next lets create a view folders and files to separate responsibilities and structure the express server app.
We use this file to configure the server before importing it into our app.js file to start listening for http requests
To keep our code neat and readable, we will create a config index file that will export each of the config files. Making for neat import statement when requiring.
Express Server App
Import our server config and start listening for http requests
Keep our secret information in the .env file and git ignore the file to avoid uploading it to the repository for the internet to find.
We require the dotenv file when starting nodemon in the package.json file below
NPM Run Scripts
Add a few npm run script to load and test the express server.
clear; will clear the terminal window before executing the npm script
nodemon we setup in post 01, and is a utility to automatic reload our node instance when a file change is detected
--require dotenv/config loads the .env file before executing the app entry point
--exec babel-node will transpile our ECMAScript project prior to executing
Testing with Mocha Chai
We are aiming to have a test driven development project, with maximum test coverage.
So we want to setup our test environment
Create Folder & File Structure
Lets setup the folder and file structure for our testing environment.
Test everything is works