🔧 Creating a Node.js Server from Scratch 🚀
Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to
Hey there, awesome devs! 👋 Have you ever wondered how web servers work? What if I told you that you can create your own server in just a few lines of JavaScript? 🤯
With Node.js, you don’t need a fancy framework to build a basic server. The built-in HTTP module lets you create a web server that can handle requests and send responses—just like a real web app! 💡
In this blog, we’ll go step by step to build a simple Node.js server. By the end, you’ll understand how servers work and feel like a backend ninja! 🥷🔥
🌍 What is a Web Server?
A web server is a program that listens for requests from a client (like a browser) and responds with data (like an HTML page or JSON). It acts as the middleman between users and your website.
✅ When you visit example.com
, your browser sends a request to a web server.
✅ The server processes the request and sends back a response (HTML, JSON, files, etc.).
✅ Your browser displays the response, and voilà! 🎉
Now, let's build our own server! 🏗️
🚀 Step 1: Setting Up Your Project
First, make sure you have Node.js installed. You can check by running:
node -v
If Node.js is installed, you'll see a version number. If not, download it from nodejs.org. ✅
Create a new project folder and navigate to it:
mkdir my-node-server && cd my-node-server
Now, let's create our server file:
touch server.js
🏗️ Step 2: Creating a Basic Node.js Server
Now, open server.js
and add the following code:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, world! 🌍');
});
server.listen(3000, () => {
console.log('Server running on http://localhost:3000 🚀');
});
✅ How it works:
- We import the
http
module. - We use
http.createServer()
to create a server. - When a request comes in, we send a plain text response.
- The server listens on port 3000.
Run the server with:
node server.js
Now, open your browser and visit http://localhost:3000
—you should see "Hello, world!" 🎉
📡 Step 3: Handling Different Routes
A real web server should respond differently based on the URL (route). Let's modify our server to handle multiple routes:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
if (req.url === '/') {
res.end('Welcome to the Home Page! 🏠');
} else if (req.url === '/about') {
res.end('About Us Page 📖');
} else {
res.writeHead(404);
res.end('404 Not Found ❌');
}
});
server.listen(3000, () => {
console.log('Server running on http://localhost:3000 🚀');
});
✅ Now try:
-
http://localhost:3000/
🏠 (Home page) -
http://localhost:3000/about
📖 (About page) -
http://localhost:3000/contact
❌ (Oops! 404 error)
🖥️ Step 4: Sending an HTML Response
Instead of plain text, let's send HTML:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>Welcome to My Node.js Server! 🚀</h1>');
});
server.listen(3000, () => {
console.log('Server running on http://localhost:3000 🚀');
});
✅ Now, visiting http://localhost:3000
will display styled HTML content! 🎨
🔥 Step 5: Serving JSON (for APIs)
Let's modify our server to respond with JSON data instead:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
const data = { message: 'Hello, this is JSON data! 🚀' };
res.end(JSON.stringify(data));
});
server.listen(3000, () => {
console.log('Server running on http://localhost:3000 🚀');
});
✅ Now, visiting http://localhost:3000
will return JSON data—great for building APIs! 📡
🎯 Conclusion
Boom! You just built a fully functional Node.js server from scratch! 🎉 Now, you understand how web servers work and can start building real-world applications!
🚀 Recap:
- Created a simple Node.js server with the HTTP module.
- Handled multiple routes (Home, About, 404 errors).
- Sent HTML and JSON responses for different use cases.
- Learned how web servers work under the hood.
In the next article, we’ll explore JSON Response, Stay tuned! 🔥
If you found this blog helpful, make sure to follow me on GitHub 👉 github.com/sovannaro and drop a ⭐. Your support keeps me motivated to create more awesome content! 😍
Happy coding! 💻🔥
...
🔧 Creating a Node.js Server from Scratch 🚀
📈 28.03 Punkte
🔧 Programmierung
🐧 Linux from scratch vs docker scratch?
📈 22.41 Punkte
🐧 Linux Tipps
🔧 Creating k-NN with C++ (from Scratch)
📈 18.74 Punkte
🔧 Programmierung
🔧 Creating a new Airbyte connector from scratch
📈 18.74 Punkte
🔧 Programmierung
🔧 Creating a design system from scratch
📈 18.74 Punkte
🔧 Programmierung
🐧 Creating a Linux distribution from scratch
📈 18.74 Punkte
🐧 Linux Tipps
🔧 Creating a hashmap from scratch in Golang
📈 18.74 Punkte
🔧 Programmierung
🕵️ Creating a Lua Decompiler from scratch.
📈 18.74 Punkte
🕵️ Reverse Engineering
🐧 Creating a linux Distro from scratch
📈 18.74 Punkte
🐧 Linux Tipps
📰 Creating SMOTE Oversampling from Scratch
📈 18.74 Punkte
🔧 AI Nachrichten
📰 Creating SMOTE Oversampling from Scratch
📈 18.74 Punkte
🔧 AI Nachrichten