01 Project – Node.js / Express / MongoDB – Part 02 – Organizing project folders and installing the dependencies

Before starting to write code you have to set up a project folder with the proper folder structure where all your files will reside. My project folder structure is shown below.

ProjectOne
***helpers
***controllers
***public
******css
******img
******js
******js
******upload
***server
***views
******layouts
******partials

Note:
The way I named the folders is pretty standard among JavaScript developers.

My project folder also contain the mongodbpath folder, and node_modules folder which were created before when we installed express framework and MongoDB database. We keep those folders!

Also, under ProjectOne folder we have two files: package-lock.json and package.json. We also keep those files!

Forder structure Node.js project

Now, we need to install the dependencies we need for our project. Open the terminal and run the following command:

npm install express morgan body-parser cookie-parser method-override errorhandler express-handlebars --save

Node.js project installing dependencies

Here is how my package.json looks like:

{
  "name": "projectone",
  "version": "1.0.0",
  "description": "My first Node.js project",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Dan Dumitrache",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.17.2",
    "cookie-parser": "^1.4.3",
    "errorhandler": "^1.5.0",
    "express": "^4.15.3",
    "express-handlebars": "^3.0.0",
    "method-override": "^2.3.9",
    "mongodb": "^2.2.28",
    "morgan": "^1.8.2"
  }
}

As you can see, all the dependencies are installed.

The next step is to create a file which will serve as the entry point of the site. I named it index.js. You can name it however you want as long as it has the .js extension.

Note:
Many developers name the entry point file server.js, index.js or home.js.

Inside index.js add the following code:

var express = require('express');
app = express();

In the first line the express module is assigned to the express variable. On the second line variable app is declared which is what express returns when it is executed. The app is the object that powers the entire applications we are going to build.

Leave a Reply