This is a simple Node.js HTTP server that responds with "Hello World!" to incoming requests.
Note: This code is an example taken from nodejs.org.
-
Clone the repository or download the source code.
-
Install Node.js if you haven't already. You can download it from here.
-
Navigate to the project directory in your terminal.
-
Install dependencies by running the following command:
npm install
To start the server, run the following command in your terminal:
Once the server is running, open a web browser and navigate to http://localhost:3000
. You should see a "Hello World!" message displayed in plain text.
The server.js
file contains the code for the HTTP server. Here's a breakdown of the code:
-
Importing necessary modules:
- The
createServer
function is imported from the Node.jshttp
module.
- The
-
Creating an HTTP server:
- An HTTP server is created using the
createServer
function. - The server responds to incoming requests with a "Hello World!" message.
- An HTTP server is created using the
-
Starting the server:
- The server is started and made to listen on port 3000 of the localhost (
127.0.0.1
). - A message is logged to the console indicating that the server is listening.
- The server is started and made to listen on port 3000 of the localhost (