Getting Started With NodeJS and Express

Several months ago I began working in Node JS. It is a really good language that supports event models at the language level. Node JS is a wrapper written around Google's V8 JavaScript engine that allows JavaScript to be run outside the browser. It optimizes JavaScript for working outside of the browser.
 
This article is all about how to start working with Node JS and how to design web applications using an Express module. There are many IDEs available in the market for writing Node JS code. I have used Intellij IDEA in this article.
 
So let's start reading this article and understand the Express framework. I assume that you have already installed Node in your machine; if not then install it from here.
Click here if you want to configure node node in intellij IDEA
 
Let's start creating an Express project; Express is a framework that allows the creation of the MVC web architecture in a Node JS application. There are many ways to create an Express project but in this article I will use Intellij IDEA to create a node Express project.
 
1. Create Express project

Open Intellij IDE and create a new project as in the following:
 

 
Click on the Finish button and intellij will start downloading the required modules and files for your Express project.
 

 

 
Click the "Configure" button to include the node core library in your project. This core library will help you to enable intelligence when writing node code.
 
Finally our Express project is ready with its predefined structure.
 

 
An Express project contains many folders and files. The above structure isn't very hard to understand, let's drill down it.

  • node_modules: An Express project uses a MVC framework and it uses many other modules that we call dependency modules for the Express framework. So all dependency modules are stored in the node_modules folder.
  • public: This folder contain CSS, JavaScript files and images.
  • routes: This folder contains a JS file that will help to route user URL. In this folder we can create many JS files and use them in an app.js file.
  • views: This is the folder where we add our jade page. This jade page will be shown in the browser.
  • app.js: This file is the heart of the Express framework. In this file we are configure the Express application settings.
  • package.json: It is a JSON configuration file where we can configure project details and its dependency, for more details click here.


 
2. Run Express project

The following are the two ways to run your Express project.

  1. Click on the "Run" button or press "shift + F10" from intellij.


     
  2. Open cmd windows and go to the Express project location and execute the following command:

    C:/express/project/location> node app.js



    Open the browser and navigate to 127.0.0.1:3000 after the Express server starts running, see the following:


References

  1. Setup MongoDB on Windows 8
  2. Node.js
  3. nodejs.org


Similar Articles