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

Configuring a Basic Server in Node.js

Configuring a Basic Server in Node.js

In this tutorial, we will learn how to configure a basic server in Node.js. Setting up a server is a fundamental step in building web applications, and understanding how Node.js handles incoming requests is crucial for creating scalable applications. This tutorial, originally created in 2016, includes some updates to keep it relevant for current development practices.

Step 1: Loading the HTTP Module

Node.js has a built-in module called http that allows us to create and configure servers. To start, we need to load this module using the require function.

Example: Loading the http Module

const http = require('http');

The http module provides several functions that make it easy to create a basic web server. Using this module, we can build a server that listens for incoming requests and sends responses.

Step 2: Creating the Server

After loading the http module, we can use it to create a server using the createServer method.

Example: Creating a Server

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

In the above code:

  • req: Represents the incoming request.
  • res: Represents the response we send back to the client.
  • res.statusCode = 200: Sets the status code to 200 (OK).
  • res.setHeader('Content-Type', 'text/plain'): Sets the content type to plain text.
  • res.end('Hello, Node.js Server!\n'): Ends the response and sends back the string 'Hello, Node.js Server!'.

Step 3: Defining the Port and Hostname

To make the server accessible, we need to specify the hostname and port on which the server will listen for requests.

const hostname = '127.0.0.1';
const port = 3000;
  • hostname: Represents the IP address of the server (in this case, 127.0.0.1, which refers to localhost).
  • port: Represents the port number the server will listen to. Here we use 3000.

Step 4: Starting the Server

To start the server, we use the listen method, passing in the port and hostname.

Example: Starting the Server

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

When the server starts, it will print a message in the console indicating that it is running and where it can be accessed.

Step 5: Testing the Server

To test your server, navigate to http://127.0.0.1:3000/ in your web browser. You should see the message 'Hello, Node.js Server!' displayed on the page.

Tip (2024 Update): Node.js now supports using ES Modules. You can replace require with import if you use an .mjs file extension or set "type": "module" in package.json.

Step 6: Stopping the Server

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

Conclusion

In this tutorial, we have successfully configured a basic server in Node.js using the built-in http module. We learned how to create and start a server, define the hostname and port, and handle basic requests and responses.

This foundational knowledge will be valuable as we move on to more complex server configurations, routing, and connecting to databases. With this understanding, you are ready to build more interactive and dynamic web applications using Node.js.

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