Starting Nodejs

Posted on  by admin

Node.js Tutorial. Node.js Useful Resources. Selected Reading. Before creating an actual "Hello, World!" application using Node.js, let us see the components of a Node.js application. A Node.js application consists of the following three important components −. Import required modules − We use the require directive to load Node.js modules.

Create server − A server which will listen to client's requests similar to Apache HTTP Server. Read request and return response − The server created in an earlier step will read the HTTP request made by the client which can be a browser or a console and return the response.

We use the require directive to load the http module and store the returned HTTP instance into an http variable as follows −.

  1. We use the created http instance and call http.createServer() method to create a server instance and then we bind it at port 8081 using the listen method associated with the server instance.

Pass it a function with parameters request and response.

Write the sample implementation to always return "Hello World".

The above code is enough to create an HTTP server which listens, i.e., waits for a request over 8081 port on the local machine.

Let's put step 1 and 2 together in a file called main.js and start our HTTP server as shown below −. Now execute the main.js to start the server as follows −. Verify the Output. Server has started. Open http://127.0.0.1:8081/ in any browser and observe the following result.

Congratulations, you have your first HTTP server up and running which is responding to all the HTTP requests at port 8081.

44 Lectures 7.5 hours . Eduonix Learning Solutions. 88 Lectures 17 hours . Eduonix Learning Solutions. 32 Lectures 1.5 hours . 8 Lectures 33 mins. 9 Lectures 2.5 hours . SHIVPRASAD KOIRALA. 97 Lectures 6 hours . Installing on Node.js on Windows or on Windows Subsystem for Linux.

Open your command line and create a new directory: mkdir HelloNode, then enter the directory: cd HelloNode.

Create a JavaScript file named "app.js" with a variable named "msg" inside: echo var msg > app.js.

Open the directory and your app.js file in VS Code using the command: code .

Add a simple string variable ("Hello World"), then send the contents of the string to your console by entering this in your "app.js" file:.

To run your "app.js" file with Node.js. Open your terminal right inside VS Code by selecting View > Terminal (or select Ctrl+`, using the backtick character).

If you need to change the default terminal, select the dropdown menu and choose Select Default Shell.

In the terminal, enter: node app.js. You should see the output: "Hello World". Notice that when you type console in your 'app.js' file, VS Code displays supported options related to the console object for you to choose from using IntelliSense.

Try experimenting with Intellisense using other JavaScript objects.

Open your command line (Command Prompt, Powershell, or whatever you prefer).

Create a new project folder: mkdir ExpressProjects and enter that directory: cd ExpressProjects.

Use Express to create a HelloWorld project template: npx express-generator HelloWorld --view=pug.