02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

The Node.js Hello World

The Node.js Hello World

In this tutorial, we will create the classic Hello World example using Node.js. This simple example will introduce you to the basics of setting up a Node.js server and responding to incoming HTTP requests. Understanding how to create a basic server is the foundation of building more complex web applications. This tutorial was originally created in 2016, and we have included some key updates to keep it current.

Step 1: Setting Up Your Node.js Environment

Before we begin, make sure you have Node.js installed on your system. You can download the latest version of Node.js from nodejs.org.

To verify that Node.js is installed, run the following command in your terminal:

node -v

If Node.js is installed correctly, you will see the version number displayed.

Step 2: Writing the Hello World Script

To create a simple server, you will need to create a JavaScript file. Let's call it app.js. Open your favorite text editor and create the following file:

// Load the HTTP module
const http = require('http');

// Define the hostname and port
const hostname = '127.0.0.1';
const port = 3000;

// Create the server
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

// Start the server and listen on the specified port
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Explanation

  • http Module: We use Node.js's built-in http module to create the server.
  • createServer: This method creates an HTTP server that listens for requests and sends a response.
  • req, res: These parameters represent the request and response objects.
  • res.end('Hello World\n'): This line sends the response 'Hello World' back to the client.

Step 3: Running the Server

To start the server, navigate to the directory containing app.js and run the following command in your terminal:

node app.js

If everything is set up correctly, you should see the following output:

Server running at http://127.0.0.1:3000/

Step 4: Viewing the Output

Open your web browser and navigate to http://127.0.0.1:3000/. You should see the message 'Hello World' displayed on the page.

Critical Update (2024): Modern versions of Node.js now support ES Modules natively. You can replace require with import if you use an .mjs file extension or add "type": "module" to your package.json.

Step 5: Stopping the Server

To stop the server, press CTRL + C in the terminal where the server is running.

Conclusion

Congratulations! You have successfully created your first Node.js Hello World server. This simple example demonstrates the basics of creating an HTTP server with Node.js. From here, you can start building more complex web servers and explore the vast ecosystem that Node.js has to offer.

Next Steps: Now that you have a basic understanding of setting up a server in Node.js, you can move on to creating routes, handling requests, and even connecting to databases. Stay tuned for more tutorials that will help you expand your knowledge and build powerful web applications.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!

What Makes Node.js Different than Other Server-Side Languages

Explore what sets Node.js apart from other server-side languages, including its non-blocking, event-driven architecture.

03:51

The Node.js Hello World

Learn how to create a simple Node.js server that responds with Hello World. This foundational tutorial will introduce you to Node.js server basics.

03:16

Loading Packages and Using Modules in Node.js

Learn how to load packages and use modules in Node.js, including importing built-in modules, installing third-party packages, and creating custom modules.

08:32

Configuring a Basic Server in Node.js

Learn how to configure a basic Node.js server using the http module. Understand core concepts and create a server to handle incoming requests.

09:48

Using http-server as a Quick Server Solution

Learn how to use http-server to quickly serve files with minimal setup. Ideal for creating a lightweight local server for testing projects.

04:53

Going Back to React

Returning to React after Node.js basics to understand full-stack integration, focusing on how backend knowledge helps with frontend React development.

02:31