This is based on a starter kit with the following characteristics:
- Local Port Number: 3001
- Language: NodeJS (with NPM)
- App server: Express JS
- Authentication: PassportJS (http://www.passportjs.org/) - username + password with Cookies (no JWT)
- API framework: Roll our own REST API (no need for automated docs)
- Database: Postgres
- ORM: Sequelize (https://sequelize.org/)
- DB migration: https://sequelize.org/master/manual/migrations.html
- Testing:
- Unit Tests: Jest (https://jestjs.io/)
- Linter: ESLint (https://eslint.org)
-
We use the Express Generator to prepare the skeleton:
npx express-generator --no-view --git
Move the generated files into an
src
folder. -
Add CORS middleware
npm install --save cors
Add these lines to 'app.js'
const cors = require('cors') app.use(cors())
-
Prepare the JavaScript linter
We installed ESLint with the StandardJS coding style.
-
npx eslint --init
-
Select "Use a popular style guide."
-
Select "Standard."
-
Select 'JSON' as the config file format.
-
If prompted, confirm the installation of the necessary dependencies.
Update
package.json
with thejslint
andjsfmt
scripts"scripts": { "jsfmt": "eslint \"src/**/*.js\" --fix", "jslint": "eslint \"src/**/*.js\"" }
-
-
Add the Jest testing framework:
npm install --save-dev jest eslint-plugin-jest
Add the ESLint configurations into
.eslintrc.js
(refer to https://github.com/jest-community/eslint-plugin-jest#readme) -
Add nodemon for dev server
npm install --save-dev nodemon
Update
package.json
accordingly to add a dev server."scripts": { "start": "NODE_ENV=production node ./bin/www", "dev": "NODE_ENV=development PORT=3001 DEBUG=app:* nodemon --ignore '*.test.js' --watch src bin/www" }
-
Debugging
Use Debug to add your debugging messages to the terminal.
const debug = require('debug')('app:users') debug('Hello World!')
-
Add DotEnv for injecting environment variables through a
.env
file.npm install --save dotenv
Copy the
env.sample
file to.env
and update the variables in.env
:cp env.sample .env
You will then be able to view the env variables via
process.env.VAR_NAME
. As a convention, environment variables are all caps and underscore case. -
Add other dependencies:
npm install --save passport passport-local sequelize pg pg-hstore
We are using Postgres as the database for this app.
- Postgres.App - The easiest way to get started with PostgreSQL on the Mac
- Postico - A Modern PostgreSQL Client for the Mac
- Postgres - Official installer for Postgres on Windows
- pgAdmin - pgAdmin is the most popular and feature rich Open Source administration and development platform for PostgreSQL
We are using Sequelize as our database ORM (Object Relational Mapping) library. You can use the CLI tool to run database migrations and to generate the models.
NODE_ENV=development ./node_modules/.bin/sequelize db:create
NODE_ENV=test ./node_modules/.bin/sequelize db:create
NODE_ENV=development ./node_modules/.bin/sequelize db:migrate
NODE_ENV=test ./node_modules/.bin/sequelize db:migrate
./node_modules/.bin/sequelize model:generate --name User --attributes firstName:string,lastName:string,email:string,passwordHash:string
./node_modules/.bin/sequelize db:seed:all
Checkout the Sequelize v5 documentation for more info: https://sequelize.org/master/