Hello World HTTP with Node.js without framework

Simple Node.js HTTP server example responding with ’Hello World’ without frameworks, ideal for learning basic server creation on port 3000.

Brief Introduction

Hello World HTTP with Node.js without framework (node)
DESCRIPTION:
A simple HTTP server written in Node.js that responds with 'Hello World' to every request, without using any external framework like Express. Ideal for understanding the basics of creating HTTP servers in Node.js.

ADDITIONAL NOTES:
This example is very simple and serves to understand how a basic HTTP server works in Node.js without using frameworks. For more complex projects, the use of frameworks like Express is recommended.

EXPLANATIONS:
- We import the native Node.js 'http' module to create an HTTP server.
- We create a server that receives requests and responds with a simple text message 'Hello World'.
- We set the HTTP response header with status 200 and content-type 'text/plain' to indicate that the response is plain text.
- The server listens on port 3000 and prints a message to the console when ready.

Example Code: node

javascript
// Import the native Node.js http module
const http = require('http');

// Create an HTTP server
const server = http.createServer((req, res) => {
  // Set status code to 200 (OK) and content-type to text/plain
  res.writeHead(200, {'Content-Type': 'text/plain'});
  // Send the response 'Hello World'
  res.end('Hello World');
});

// The server listens on port 3000
const PORT = 3000;
server.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
});
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Node.js: JavaScript runtime required to execute the code.

Node.js 'http' module: native module used to create the HTTP server.

No external dependencies: no additional packages are required.

Configuration: the server listens on port 3000 (modifiable in the code).