Beginner’s Guide to Server Side Web-Development Leveraging Node.Js

Beginner’s Guide to Server Side Web-Development Leveraging Node.Js

 

Client-side web development sufficed to create static websites that users could request from their browsers. The files would be fetched from the web server hosting the website, for the user to view the website. This wouldn’t suggest what the server could do based on the inputs by the user.

For dynamic websites, it wouldn’t make economic sense to display all the dynamic information on separate static pages. That’s where server-side web development comes handy. You can make a few static templates using JavaScript, HTML, CSS, etc. and then update the content of the templates for each user based on user input.

Using Node.Js for Server-Side Web Development

Node.Js is a server-side cross-platform run-time environment (RTE) that runs JavaScript on both the server-side and the client side. Being a cross platform RTE, Node.Js is perfectly compatible with Windows, Mac, Linux and Solaris. To install Node.Js on Windows, you can download a pre-built installer for your version of the OS. The source code is also available in the same “download” link above.

Installation can leave you with a “node” icon in the “nodejs” folder in Program Files or the location you chose to install it. You may also find a Node.js icon or Node.js Command Prompt in your Start Menu. The Command Line is also called the REPL (Read-Eval-Print-Loop)–a high level language shell or an interactive programming environment. You can also use your own default Command Prompt. Using any of them, you can test the installation by typing “node -v” and pressing Enter. You will see the Version Number displayed like v10.15.1.

Testing Your Node.Js Installation

Now, it’s time to test if Node.Js itself works. Type “node” in the command line, which will display “>”, the greater than sign. This means that you have entered the node Command Line or that it is ready. Type the command “console.log(‘Node Install Successful’); It will display your message “Node Install Successful”. If the message returns, you can be sure that your Node.Js installation works pretty fine.

Installing and Testing NPM Package Management and Express.Js

The NodeJs installation using the above installer also installs NPM Package Management along with NodeJs. You can also test NPM installation by typing “npm -v”, which will display the NPM version number like 6.4.1.

With NPM v6.4.1, the Express modules come in the package.json file, under the dependencies list. Running the “npm install” also automatically installs the modules in the dependencies list.

Meanwhile, you can also download and install Git to use the Git Terminal in three different forms as Git CMD, Git Bash and Git GUI.

For a start, let’s see how you can create a simple localhost server (using Git Bash) in five easy steps:

  1. Open the Git Bash terminal and type:

mkdir newapp

  2. This creates a folder, after which you can create a file newapp.js using the Linux “touch” command, which works with Windows, if Git Bash is used.

cd newapp

touch newapp.js

  3. Then, open your newapp.js file from the folder and paste the below script in your newapp.js file:

const http = require(‘http’);

var server = http.createServer(function (request, response) {
response.writeHead(200, {“Content-Type”: “text/plain”});
response.end(“
New App\n”);
});

server.listen(9000);

We use “http” to create an http server, also by using a variable function called createServer. We also use other functions as request, response and the input content that will return with the response. We’ve chosen port 9000 and you can choose any port of your choice.

   4. Now, come back to the terminal and type:

node newapp.js

   5. Then, open to your browser and type:

http://localhost:9000

You will see the text “New App” appear on your browser tab.

The above is the first step in using Node.Js through which you create an HTTP server.

In our example above, we haven’t used Express.Js. Express is a web application framework that uses templates, routes (roughly URLs), route handlers (requests), etc. to generate dynamic web pages for each user. In the next part of this article, we will cover how we can create a simple dynamic website using Node.Js with the web application framework Express.Js.

Contact us at sales@twilightsoftwares.com and Visit www.twilightitsolutions.com

No Comments

Post a Comment

Comment
Name
Email
Website