I recently started working on node.js and would like to share the important details of the same with you through this series – Learning Node.js and Express.
As a first step, we would download node.js in our system. In order to do so, go to the following url.
Simply click on the download to start with it and then install the same.
After installation, let’s check the current working of the same.
Go to the Start Menu and search Node.js. Select the same, it will open as a Command Prompt.
Write statements like x=10 and press enter, you should get the output as 10. Also, try to write something like console.log(“Learning Node.js”). You should get the following output:
This means that your node.js is now working. We will now install Express. It helps us provide many features for web and mobile development. In order to install express, we just need to enter the command in the same cmd as above : npm install express. You will get the messages accordingly in the cmd itself after installation is complete.
Now, I have been using the trial version of WebStorm IDE to work on node.js. There are other editors as well, but its my personal favorite.
Let’s make a new project in WebStorm IDE. The simple steps for the same are:
- If you are opening the IDE for the first time, then you will get “Create New Project” option directly. As I have been working on it so it loads the last project automatically. So, for me the option will be to go to File, then New Project.
- Select the project type as ‘Node.js Express App”. The screen will be as in the following screenshot:
You can select the desired location of the project. The Node interpreter and Npm executable would be detected automatically by the IDE. Also, select the version of the Express that you will work on. I am working on version 4.11.0 for now.
- So after clicking on Create, you might get the following option:
Let the settings be as they are and click Configure. The IDE will automatically download all the required packages for its execution.
I have named the project as
Node.jsLearningNodeJs_1. You can find the same attached to this article as well.
The solution will look like the following:
It will contain project files as well as some external files which attaches it to the predefined node.js files that resides in the C:/Program Files/nodejs/node_modules folder (basically the installation folder)
Let’s see the importance of some of the following files:
- App.js: This file contains all the processes that needs to be executed whenever the application starts. It includes different packages like express, etc and paths to folders within the project directory.
- Routes: The files inside these folders does all the magic of talking to the DB and getting request from the user and providing response to the same after calculations as required.
Let’s see the predefined index.js file in this folder:
Please find brief information for the same in the following screenshot:
The same variable “title” is then been used in the index.jade file located in the Views folder.
Let’s run the solution and view the output.
To run the solution, click the solution name in the left window and then from the Run option in the top menu bar, click run ‘bin\www’:
Let's check the output, on browser by typing https://localhost:3000.
This is the output received and its as per the parameters passed by us from index.js to index.jade.
I hope the above article, helped in starting with nodejs and express.
In my next article, I would share the db connections and api creation in nodejs.
Thanks