Building an e-Commerce website with Node – Part 05 – Start building the application

First, open your terminal and go to the project folder (in my case it is on Desktop and it is called MyEshop). You do that in the terminal with the command cd.

Next, you want to initialize the project folder by typing the command npm init in your terminal. Once you have done that you would see a few lines of text. They are just instructions for further possible commands.

Initializing the project folder

The last line says “name: (MyEshop)”. You have to edit it because it can’t contain capital letters. I named it “myeshop”. Press “Enter”.

The next line is “version: 1.0.0”. Let it unchanged and press “Enter”.

On the next line you can give the shop a description. Type a short description then press “Enter”.

On the next line you have to define the entry point. I named it “server.js” (you can name it as you wish) then click “Enter”.

On the next three lines you have “test command:”, “git repository:”, “keywords:”; just press “Enter”.

On the next line you have “author:”. Here you should type your name.

Set your app shop

Once you are done, you are asked for confirmation “Is this ok? (yes)”. Type “yes” and press “Enter”. Now, if you type the command ls to list all the files within the current folder you will see that you have a file called “package.json”.

“package.json” holds all the dependencies you are going to use. All the libraries you are going to use will be saved in this file.

create package.json file

Now, you can start using you favorite text editor. I use Atom but any other text editor will do just fine.
Within the project folder (MyEshop) create the server.js file. First thing you want to do is to require the express library which has many methods you are going to use when creating an application with Node.js.


var express = require('express');

var app = express();

app.listen(3000, function(err) {
  if (err) throw err;
  console.log("My server is OK");
});

Explanations:

var express = require('express'); requires the library you want to use (in this case the library is “express”). The library is not yet install on the server. So at this point, if you run the server it will crush.

var app = express(); means that app is referring to express objects (you will see it in action soon).

app.listen(3000); it runs the server. 3000 represents the port. The function that follows “3000” checks for errors and if there is an error it shows the error otherwise the message “My server is OK” will be displayed. It is like a validation for your server.

Since you need express library, you have to install it first by typing the command npm install express --save in the terminal. --save means that the library will be saved in the package.json file.

Install express in terminal

If you check the package.json file you will see the express library saved there.


{
  "name": "myeshop",
  "version": "1.0.0",
  "description": "My eshop built with NODE.",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Dan",
  "license": "ISC",
  "dependencies": {
    "express": "^4.15.3"
  }
}

The last line of code lists the dependencies, now it is only the express version 4.15.3.

Also, a new folder is automatically created, called node_modules. You can see it inside your project folder (MyEshop). It contains the library itself.

Now, you want to check if the server is running correctly. Open the terminal and type the command nodemon server.

Node.js server running in terminal

As you can see, the server is running correctly and the message from the code, “My server is OK”, is displayed.

Leave a Reply