Saturday, April 27, 2024
spot_imgspot_img

Topics

spot_img

Related Posts

Node.jsĀ Get Started

Download & Install Node.js

Follow official Node.js guide for Node.js installation on your environment: https://nodejs.org

Getting Started

Once you have downloaded and installed Node.js on your computer or laptop, let’s run “Hello World” program.

Create a Node.js file named “helloworld.js”, and add the following code:

helloworld.js
varĀ http = require('http');
http.createServer(functionĀ (req, res) {
Ā Ā res.writeHead(200, {'Content-Type':Ā 'text/html'});
Ā Ā res.end('Hello World!');
}).listen(8080);


Save the file on your computer at your preferred folder. Example: C:\Users\Your Name\helloworld.js

The code tells the computer to write “Hello World!” if anyone tries to access your computer on port 8080.

Run Code via Command Line Interface

Node.js files must be run in the “Command Line Interface” program of your computer.

Open the command line interface on your computer.

Navigate to the folder that contains the file “helloworld.js“.

Now run the the program by entering following command: node helloworld.js

Now, your computer works as a server!

If anyone tries to access your computer on port 8080, they will get a “Hello World!” message in return!

Start your internet browser, and type in the address: http://localhost:8080 you will get “Hello World!” message.


LEAVE A REPLY

Please enter your comment!
Please enter your name here

Popular Articles