Setup Node project from scratch (Part 2)

Dec 10, 2018

In this post, we’ll list some tools for development and basic configuration for our project.

Required Packages/Tools

NodeJS: our main character, of course. As most of our packages/tools in this series, we’ll the latest version (except for MySQL database, I’ll later). For Node, we’ll version 11.4 with fancy ES6 syntax included.

Express: as their website, Express is a “Fast, unopinionated, minimalist web framework for Node.js”. You can imagine that if NodeJS is a skeleton, so Express is our warrior server that helps to handle request/response with clients.

NPM: (Node Package Management) for the package management (both server, frontend, and development packages). Maybe in the future, we will evolve to use Yarn, but NPM is enough for now.

NVM: (Node Version Management) is a tool that allows you to download and install NodeJS easily.

MySQL: a powerful open-source database that we’re going to use for storage.

Sequelize: a database ORM that helps to interact with MySQL database.

Jest: a powerful test framework from Facebook. Most of the time the APIs unit test articles for NodeJS are about using Mocha + Chai + Istanbul, but for me who’s using Jest framework every day to test my ReactJS front-end project, Jest can cover all these things. So we only need one package instead of installing several packages for testing or checking test coverage if needed.

Docker: helps to create a standard environment for development and easy to deploy to production later.

Postman: a Chrome app that helps to send request to our API server.

Setup Node

NVM setup

$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash

Verify installation successfully

$ nvm --version

nvm install
nvm version

List all node’s available versions

$ nvm ls-remote

We’ll see a very long list node’s available versions. Please pick up the latest version to install

$ nvm install 11.4.0

Verify installation

$ node -v

Create a project folder

Next step, we’ll create a project folder

$ mkdir time-log-project
$ cd time-log-project

We recommend linking our project to git repository (e.g: github) as soon as possible. As professional developers, that helps us tracking code better from the beginning.

Initialize package.json file for our application. In case you’re curious about how package.json work, please visit their official website.

$ npm init

It’ll prompt for several things: package name, version, description, etc… This process takes a very short time to fulfill these questions.

Our package.json looks like this after setup

initialize package
initialize package

Express setup

Now we’ll install Express and it’s dependencies

$ npm install --save express body-parser morgan

The --save option will save these packages to dependencies section of our package.json file.

At the root folder time-log-project create a file name app.js. This file plays an entry door role in our application.

const express = require('express');
const logger = require('morgan');
const bodyParser = require('body-parser');
// This will be our application entry. We'll setup our server here.
const http = require('http');
// Set up express app
const app = express();
// Log requests to the console
app.use(logger('dev'));
// Parse incoming requests data (https://github.com/expressjs/body-parser)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Setup a default catch-all route that sends back a welcome message in JSON format.
app.get('*', (req, res) => res.status(200).send({
  message: 'Welcome to our first success',
}));
const port = parseInt(process.env.PORT, 10) || 8000;
app.set('port', port);
const server = http.createServer(app);
if (process.env.NODE_ENV !== 'test') {
  server.listen(port);
}
module.exports = app;

Basically, our Express server will listen at port 8000. The configure

if (process.env.NODE_ENV !== 'test') {
  server.listen(port);
}

is optional that we don’t want to server listen on any port in test environment.

One more step before we start our server. We’ll use nodemon package, that’s a tool helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.

$ npm i -D nodemon 
  • i flag stands for install
  • -D flag stands for --save-dev that helps to save package to devDependencies section in our package.json

We’ll change our package.json script a little bit

{
  "name": "time-log-project",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "start": "nodemon ./app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "mr.leo",
  "license": "ISC"
}
  • We indicate start file app.js in script instead of main.
  • Using nodemon to start app by default.

Time to run our application

$ npm start

and visiting http://localhost:8000 to see a message

{"message": "Welcome to our first success"}

That’s all basic setup project step. In the next step, we’ll install and configure Sequelize work with MySQL and Docker together. See you in the next section.