Node is a platform for building fast and scalable server applications using JavaScript.
Install Node for your platform:
One can download the Community Edition since it is free for study.
After installation, now create one new project in Visual Studio.
You can see the separate JavaScript language tab.
Now using the search Nugget package for nodejs.
After completion of the installation restart Visual Studio and select the JavaScript tab. You will be able to see the following projects.
Node.js Express
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
If you need more information on Express then please find the following link:
We can create a blank Node.js application and add the express script or we can create a new application using Node.js Express.
For this time I am creating a Basic Node.js Express application.
We will go through some new terms.
- npm: npm is a NodeJS package manager. As its name implies, you can use it to install node programs. Also, if you use it in development, it makes it easier to specify and link dependencies. If anyone needs to add new dependencies to the project then he can add those using npm.
The way to add is to right-click on npm and then do as in the following:
You can install packages using “Install new npm packages”.
To get a better understanding of npm packages please go through https://www.npmjs.com/.
- Express: As I said earlier, this is the web framework for node.js.
- Jade: jade is a template engine for node.js and the default rendering engine for the express web framework. We can say it is an alternative to HTML.
Example
extends layout.
block content.
h1= title
p Welcome to #{title}
If you need more details on Jade then please go through http://jade-lang.com/.
- App.js: This is the default JavaScript file.
- http.createServer(app).listen(app.get('port'), function () {
- console.log('Express server listening on port ' + app.get('port'));
- });
- package.json: The package.json file is normally located at the root directory of a Node.js project. The name field should explain itself. This is the name of your project. The version field is used by npm to ensure the right version of the package is being installed.
- Routes: Routing refers to determining how an application responds to a client request to a specific endpoint, that is a URI (or path) and a specific HTTP request method (GET, POST, and so on). Each route can have one or more handler functions, that is / are executed when the route is matched. Route definition takes the following structure app.METHOD(PATH, HANDLER), where the app is an instance of express, METHOD is an HTTP request method, PATH is a path on the server and HANDLER is the function executed when the route is matched.
Example
- exports.index = function (req, res) {
- res.render('index', { title: 'Express' });
- };
To understand more on routes please go through http://expressjs.com/guide/routing.html.
When you start the application then:
- Server starts locally:
*Server start
The following is the page as shown in a browser: