Skip to main content

Java

Node.js Hello World: Install and First Server

·

Terminal window showing the Node.js server running at localhost:8081

Node.js runs JavaScript on the server side using the V8 engine. It handles many concurrent connections without blocking, which makes it a practical choice for real-time applications and APIs.

Install Node.js

Download the installer from nodejs.org. Run it, accept the defaults, and click through the prompts. Once the installer finishes, open a terminal and confirm the install:

node --version

You should see a version number like v22.x.x.

Write the HTTP server

Open a text editor and paste the following code:

var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World!');
}).listen(8081);

console.log('Server running at http://localhost:8081/');

Save the file as helloworld.js.

Run the server

Open a terminal:

  • Windows: press Windows + R, type cmd, press Enter
  • Linux: press Alt + F2, type gnome-terminal, press Enter

Then run:

node helloworld.js

Open a browser and go to http://localhost:8081/. You see the text "Hello World!" in the page.

Add basic HTML output

The server above sends plain text. To send HTML instead, change the Content-Type header and put markup in response.end:

response.writeHead(200, {'Content-Type': 'text/html'});
response.end('<html><body style="background:#303030"><h1>Hello World!</h1></body></html>');

Restart the server (Ctrl+C, then node helloworld.js again) and reload the browser. The page now renders the heading against a dark background.

Where Node.js is used

Node.js powers the backend of platforms including Microsoft Azure, Yahoo, LinkedIn, and eBay. Once you understand the event loop and the http module, you can build REST APIs, WebSocket servers, and command-line tools with the same language you use in the browser.

For deeper work on JavaScript assignments, our programming homework help team covers Node.js, Express, and REST API tasks. Related reading: Functional Programming in JavaScript and How React.js Works.

Share: X / Twitter LinkedIn

Related articles

  • Java

    Java Swing Tutorial for Beginners

    Learn Java Swing from scratch: build your first window, wire button events, master five layout managers, and assemble a working calculator GUI.

    May 24, 2024

  • Java

    Advanced Java Data Management Techniques

    Master advanced Java data management: optimize data structures, handle concurrent access, tune memory, and use serialization and compression in real applications.

    May 3, 2024

  • Java

    Java File I/O: Read, Write, and Manage Files

    A practical guide to Java file I/O: streams, readers and writers, NIO Path and Files, buffering, serialization, and the exceptions that break file code.

    Oct 7, 2023

← All articles

Stuck on a programming assignment?

Get expert help in Java, C++, Python, JavaScript, SQL, and more. We deliver working code with a clear walkthrough so you can understand and defend it.