In this tutorial, we will learn how to build a professional application for job announcement using Node.js, Express.js web framework, and MongoDB database. We will also learn how to deploy our app to Heroku platform. 

 

First of all, we need to create a folder for our app, for example “khmerweb-job” folder, and open it with Visual Studio Code. In this IDE (Integrated Development Environment) we could launch a Terminal window by clicking on Terminal > New Terminal on the menu. All Node.js applications require an initial file called “package.json” to initiate the application. To create this json file, we just write on the terminal window as below:

 

npm init

 

NPM (Node Package Manager) is a software included in Node.js package when we download and install this package in our computer. And when we initialize package.json file, NPM will ask us to enter necessary information for our application. We do not need to enter all information, we can press Enter key instead to jump to the next question. After asking all questions,  the package manager NPM will create a package.json file as below:

 

//package.json

{
  "name": "khmerweb-job",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.1",
    "nodemon": "^2.0.16"
  }
}

 

As we will use ES6 style to write the codes for our app, we must add “type”:"module" to the package.json file. We also need to tell Node.js which file we will use as the entry point for our application. In this case, we could use index.js file in the root folder as the entry point. To achieve this, we need to add  "start": "nodemon index.js" to the package.json in the “scripts” section.

 

//package.json

{
  "name": "khmerweb-job",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon index.js"
  },
  "author": "",
  "license": "ISC"
}

 

As we just tell Node.js that we will use nodemon program to run the code in index.js file that is the entry point of the application, it requires us to install this program first.

 

npm install nodemon

 

//package.json

{
  "name": "khmerweb-job",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "nodemon": "^2.0.16"
  }
}

 

After installing nodemon program, we see that the NPM package manager has been adding nodemon program as a dependent package to package.json file. Every time we install a new package, NPM will include it as a dependent package in package.json file. This is very useful when we deploy our app to a server online - the sever will read package.json file to install all dependent packages for our app. Comparing to Python requirements.txt file, we need to update it manually whereas package.json is automatically updated by NPM.

 

The advantage of using nodemon to run Express.js program is that every time we modify our code, nodemon will restart the server to reflect to result from the modified code. If we use Node.js to run the server, we have to manually restart the server. However, using nodemon is only in development phase, when we deploy our app to a server, our app will be in production mode, we need to use node instead of nodemon by changing it in the package.json file.

 

Next, we need to also install Express.js web framework by writing on Terminal window as below:

 

npm install express

 

After creating an index.js file in the root folder to be used as the entry point, we could write a minimal Express.js application as below:

 

import express from 'express'
const app = express()

const port = process.env.PORT || 8000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`This app is listening to the port ${port}`)
})

 

To run the above program, we can write on Terminal window as below:

 

npm start

 

If we open the browser at the address http://localhost:8000 we will see the message “Hello World!” is written on the browser.