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.

Video Transcript

00:01We spent the last few chapters getting ready for this one.
00:04So let's not do too much theory
00:06because what we're gonna do here is create our first hello
00:08world no JS application as we created
00:12and output hello world into the world.
00:16So let's jump right into it.
00:18So I created a new empty folder
00:21and I encourage you to do the same.
00:22You don't need the source files.
00:24And let's go ahead and create a new node JS package.
00:28Now, a node JS package is oto an NPM package
00:33'cause we're using NPM to create it.
00:35And we already saw with NPM that we just call
00:37that in it function.
00:39And I'm gonna skip the interview
00:41and it will automatically create
00:43for me the package dot js o file.
00:45And I could go ahead and then change if I want something in
00:48the name or the version.
00:49Right now we're fine with this version,
00:51but we might want to go back
00:52and say, no, this is not publicly ready yet.
00:54So we're still in version zero.
00:56But in reality what we'll probably do is just work locally
00:59until we're ready and then release it to the world.
01:03Um, we don't need to fill up any other information here.
01:05We're fine with everything in here.
01:07So I'm gonna go ahead and save that.
01:09Now, once we have a project, the next step
01:11for us is actually to define our main JavaScript file,
01:15which is where our actual no JS application will reside.
01:21So I'm gonna, I want to create that, uh, index that, uh, j
01:27index js file.
01:29Now, I could go ahead and work with V
01:32or work with some sort of a terminal type of um, uh,
01:37editor, but I really don't like to, and most people don't
01:39unless you're a real heavy server side developer in general,
01:43you really just want to create the file
01:45and work with it with an
01:46editor that you're comfortable with.
01:47I like working with Sublime Text, so I'm just gonna go ahead
01:51and touch that index js file just to create the file.
01:55And once I've created the file,
01:57I'll see in my folder also the JS file
02:00and I could then I'm gonna drag it into, oops,
02:03I'm gonna drag it into our editor and we have our, our file.
02:08Now all I'm gonna do is I'm gonna call here,
02:11I'm gonna call console log.
02:14Hello world.
02:15The most simple, most primitive application one line
02:20that all it does is outputs to the console.
02:23Hello world. For me to test this application out
02:26and see if it actually works, I'm gonna have
02:28to go back into my terminal.
02:30And in the terminal itself, instead
02:32of writing NPM like we've been doing so far,
02:35I'm gonna write node
02:36and the name of the file that I want to initialize
02:39or that I wanna run.
02:40And if I go ahead and I run it,
02:42it's gonna run our application and terminate
02:44and say hello world.
02:45And there you go. You just created your first most simple
02:49idiotic application.
02:51And let's step forward in the next lecture
02:53where we'll learn about really the core
02:56feature competency. One of the coolest things
02:58Really that are really powerful and no JS
03:01and NPM in general is how do you use packages
03:04that you install and you add In this lecture,
03:07we created our first NO JS application,
03:10and in the next one we're gonna take a step deeper
03:12as we load in packages and use modules.

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!