Getting Started with Node.
js: Creating a Simple Web Server
This document will guide you through the process of setting up and running a basic web
server using [Link]. This is a fundamental first step for building any web application with
[Link].
Prerequisites
Before you begin, make sure you have the following software installed on your machine:
[Link]: The JavaScript runtime environment. You can download it from the official
[Link] website.
npm: The [Link] package manager, which is installed automatically with [Link].
Step-by-Step Guide
Step 1: Initialize Your Project
First, create a new directory for your project and navigate into it using your terminal or
command prompt.
mkdir my-first-server
cd my-first-server
Next, initialize a new [Link] project. This command creates a [Link] file to manage
your project's dependencies and metadata.
npm init -y
The -y flag answers "yes" to all the default questions, making the process faster.
Step 2: Write the Server Code
Create a new file named [Link] in your project directory. Open this file in your preferred
code editor and add the following code:
// Import the 'http' module, which is a core [Link] module
// for creating HTTP servers.
const http = require('http');
// Define the hostname and port for the server.
const hostname = '[Link]'; // This is localhost
const port = 3000;
// Create the server instance.
const server = [Link]((req, res) => {
// Set the HTTP status code to 200 (OK).
[Link] = 200;
// Set the HTTP header to indicate the response content type.
[Link]('Content-Type', 'text/plain');
// Send a response to the client.
[Link]('Hello, World!\n');
});
// Start the server and listen for incoming requests on the specified port.
[Link](port, hostname, () => {
// This callback function runs when the server is successfully started.
[Link](`Server running at [Link]
});
Step 3: Run Your Server
To start your server, go back to your terminal and run the [Link] file using the node
command.
node [Link]
You should see the message Server running at [Link] printed in your
terminal. This means the server is running and listening for requests.
Open your web browser and navigate to [Link] You should see the text
"Hello, World!" displayed on the page.
Explanation of the Code
const http = require('http');: This line imports the built-in http module, which
contains the functionality needed to create an HTTP server.
[Link](...): This method creates a new server object. It takes a single
argument, which is a function that will be called every time a request is received.
(req, res) => { ... }: This is the request handler function. It receives two objects: req
(the request object) and res (the response object).
[Link] = 200;: This sets the HTTP status code. A status of 200 means the
request was successful.
[Link]('Content-Type', 'text/plain');: This sets a header on the response,
telling the browser that the content it's about to receive is plain text.
[Link]('Hello, World!\n');: This sends the response body to the client and signals
that the response is complete.
[Link](...): This method starts the server and makes it listen for connections on
the specified hostname and port.
Next Steps
Now that you have a basic server running, you can explore more advanced topics like:
Serving HTML files: Instead of plain text, serve a full HTML page.
Handling different routes: Respond with different content based on the URL the user
requests (e.g., /about, /contact).
Using frameworks: Learn how to use popular frameworks like [Link] to simplify
server creation.