What is Node?
This blog will introduce you to the NODE. I will take you through Node and its environment set up for developing your first application. Node is an open-source, cross-platform runtime environment for executing the JavaScript code. For Node, you must be acquainted with JavaScript, HTML, and CSS and also have some familiarity with web applications and the MVC concept. Its built on the Google V8 JavaScript engine and V8 compiles JavaScript code to native machine code.
Before Node, JavaScript was only executed in browsers. In 2009 Ryan Dahl used an open-source Google V8 JavaScript engine to build Node as a runtime environment for JavaScript outside a browser.
So using Node it is possible to use JavaScript as a server-side language.
By using Node we can develop the MVC based web applications. With Node, however, you use JavaScript everywhere, on the server-side as well as on the client-side.
For more information about Node follow the link below:
https://nodejs.org/en/
Setting up the Environment
I took the reference of the given link https://blog.udemy.com.
For developing a Node-based application there are a few tools, which we have to install in our system. Here is a list of some basic tools which we need for Node application.
Install Text editor to build your code. Here is a list of text editor which you can install on your system.
https://notepad-plus-plus.org/download/v6.8.5/
http://www.sublimetext.com/
Or you can use your Notepad editor.
Install MongoDB
https://www.mongodb.org/
After installing Mongo either you can run it from command prompt or window service.
Install Node
https://nodejs.org by going to the given link you will get NPM (Node Package Manager) on your machine. By using NPM we can download packages/modules in our applications.
Install Express Generator
Express Generator is a Node module that we use to scaffold (platform) an application. To install Express Generator open a new Command Prompt and run the given command
Create a platform of your project which you want to develop using Node.
Enter the given command on command prompt after installing Express Generator,
Express project Name
Example: – express hello here hello is my project name.
Now open your project in your favorite text editor and the directory structure of your application will be like this:-
Public is where we store public assets of our website, such as Javascript files, stylesheets, images, etc.
Routes defines an endpoint in your application where you receive requests.
View includes the view files for your application. Express supports many popular template engines such as Jade, Html, EJS, and Handlebars, etc. Jade is the default template engine for Node.
App.js is the entry point to your application. Its where your application is defined and configured. This is the controller of your application.
Package.json this includes application metadata and identifies application dependencies. Here you will see your package.json structure:-
Here we will specify name and the version of our application as well as its dependencies.
Install Dependencies
To install the dependencies, go to terminal or command prompt and run the following commands:
Npm install
To know more about the packages follow the link below:
https://www.npmjs.com
node_module is the folder in your application directory where all modules will be stored.
Install Nodemon
To install Nodemon follow the given command
Npm install nodemon -g
When you start a Node application, a basic web server starts on port 3000 and accepts incoming requests. If you make any changes in your application, you need to restart your application server so this is a time taking process to start the server after every alteration. So in order to overcome we use Nodemon, which is another Node module that automatically restarts your web server whenever it detects any change in your source files.
Install Monk
Monk is a Node module for reading and writing documents in MongoDB
To install monk follow the given command
Npm install monk save
How to run your application?
For running this basic application run the following command on your project directory on command prompt
Go to project by using
Cd hello
Then enter the command to run application
Nodemon
Nodemon will start your web server on port 3000, as shown in the above image.
Now launch your browser and navigate the given link:
http://localhost:3000
Here is your first express application.
For more information about Node you can refer to the link below:
http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/
http://code.tutsplus.com/tutorials/nodejs-step-by-step-blogging-application–net-20496
http://www.theprojectspot.com/tutorial-post/Node-js-for-beginners-part-1-hello-world/2
Thanks,
Anil Singh
Leave A Comment