Node Js Application

Posted on  by admin
  • In our previous tutorial, we have successfully installed Node.js.
  • Now it is time to write a simple Node.js application and climb the learning curve.
  • In this tutorial, we shall look into an example Node.js Application where we shall build an HTTP Server; and understand some of the basic components that go into making of a Node.js Application.

We shall create a basic HTTP server listening on a port. You may use any text editor of your choice to create a script file with extension .js . Create a file with the name node-js-example.js and copy the following content to it.

  • Once you have created the file, you may run it using node program from command line interface or terminal.

  • If you see nothing echoed back to the terminal, it is working fine, and a HTTP server has been started listening at port 8087.

  • Now open any of the browser and hit the url http://localhost:8087/ to see the server responding with html page containing content Node.js says hello!

Great code editing

Hello World

Let us look into the two lines of code that we have written in this example. This is just a mere example on how to get started with Node.js.

Deploy your application

In our subsequent tutorials, we shall learn about these concepts in details. Node.js is a platform for building fast and scalable server applications using JavaScript. Node.js is the runtime and npm is the Package Manager for Node.js modules. Visual Studio Code has support for the JavaScript and TypeScript languages out-of-the-box as well as Node.js debugging.

However, to run a Node.js application, you will need to install the Node.js runtime on your machine. To get started in this walkthrough, install Node.js for your platform.

An Express application

The Node Package Manager is included in the Node.js distribution. You'll need to open a new terminal (command prompt) for the node and npm command-line tools to be on your PATH.

To test that you have Node.js installed correctly on your computer, open a new terminal and type node --version and you should see the current Node.js version installed.

Linux: There are specific Node.js packages available for the various flavors of Linux.

Next steps

See Installing Node.js via package manager to find the Node.js package and installation instructions tailored to your version of Linux.

Windows Subsystem for Linux: If you are on Windows, WSL is a great way to do Node.js development.

Debug your Express app

You can run Linux distributions on Windows and install Node.js into the Linux environment.

Debugging Hello World

When coupled with the Remote - WSL extension, you get full VS Code editing and debugging support while running in the context of WSL.

To learn more, go to Developing in WSL or try the Working in WSL tutorial.

Let's get started by creating the simplest Node.js application, "Hello World".

Integrated Terminal

Create an empty folder called "hello", navigate into and open VS Code:.

Tip: You can open files or folders directly from the command line.

refers to the current folder, therefore VS Code will start and open the Hello folder.

Running Hello World

From the File Explorer toolbar, press the New File button:.

and name the file app.js:.

By using the .js file extension, VS Code interprets this file as JavaScript and will evaluate the contents with the JavaScript language service.

Example – Node.js HTTP Server

Refer to the VS Code JavaScript language topic to learn more about JavaScript support.

Create a simple string variable in app.js and send the contents of the string to the console:.

Note that when you typed console.IntelliSense on the console object was automatically presented to you.

Learn NodeJS Step by Step

Also notice that VS Code knows that msg is a string based on the initialization to 'Hello World'.

If you type msg.

you'll see IntelliSense showing all of the string functions available on msg.

Master NodeJs : A Practical Approach To Node JS

After experimenting with IntelliSense, revert any extra changes from the source code example above and save the file (⌘S (Windows, Linux Ctrl+S)).

It's simple to run app.js with Node.js.

From a terminal, just type:.