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!