- Home
- Services
- IVY
- Portfolio
- Blogs
- About Us
- Contact Us
- Sun-Tue (9:00 am-7.00 pm)
- infoaploxn@gmail.com
- +91 656 786 53
Node.js 101: Your First HTTP Server
If you’re new to Node.js, one of the first things you’ll want to try is building a simple HTTP server. Node makes this ridiculously easy because it ships with a built-in `http` module—no extra packages required.
Why Node.js for Servers?
Node.js is built on Chrome’s V8 JavaScript engine and is designed around non-blocking, event-driven architecture. In plain English: it’s fast, lightweight, and perfect for building web servers that can handle lots of requests without choking.
Setting Up Your First Server
You don’t need anything fancy to start. Just make sure Node.js is installed. Run this in your terminal to check:
node -v
If you see a version number, you’re good to go.
Now, let’s write our first server. Create a file called server.js and paste this in:
const http = require(’http’); // Create a server object const server = http.createServer((req, res) => { res.statusCode = 200; // OK res.setHeader(’Content-Type’, ’text/plain’); res.end(’Hello, world!\n’); }); // Start the server const PORT = 3000; server.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}/`); });
Save it, then run:
node server.js
Visit http://localhost:3000 in your browser. You’ll see: Hello, world!
Congratulations—you’ve just built your first HTTP server with Node.js.
Understanding What’s Happening
- http.createServer() spins up a new server.
- The callback (req, res) runs every time someone hits your server. req is the incoming request, and res is what you send back.
- server.listen(PORT) keeps the server running on the port you specify.
Making It a Bit Smarter
const http = require(’http’); const server = http.createServer((req, res) => { if (req.url === ’/’) { res.writeHead(200, { ’Content-Type’: ’text/plain’ }); res.end(’Welcome to the homepage!\n’); } else if (req.url === ’/about’) { res.writeHead(200, { ’Content-Type’: ’text/plain’ }); res.end(’This is the about page.\n’); } else { res.writeHead(404, { ’Content-Type’: ’text/plain’ }); res.end(’Page not found.\n’); } }); const PORT = 3000; server.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}/`); });
Now you’ve got basic routing:
- / → homepage
- /about → about page
- Anything else → 404
Where to Go From Here
- Learn about frameworks like Express.js for easier routing and middleware.
- Explore serving JSON instead of plain text (hint: res.setHeader(’Content-Type’, ’application/json’)).
- Add logging or environment variables to manage configuration.
Final Thoughts
Node.js makes spinning up a web server fast and simple. With just a few lines of code, you can handle requests and build the foundation for larger applications. Once you’re comfortable with the basics, frameworks like Express or Fastify will open even more possibilities.
Imagine reducing your operational costs by up to $100,000 annually without compromising on the technology you rely on. Through our partnerships with leading cloud and technology providers like AWS (Amazon Web Services), Google Cloud Platform (GCP), Microsoft Azure, and Nvidia Inception, we can help you secure up to $25,000 in credits over two years (subject to approval).
These credits can cover essential server fees and offer additional perks, such as:
By leveraging these credits, you can significantly optimize your operational expenses. Whether you're a startup or a growing business, the savings from these partnerships ranging from $5,000 to $100,000 annually can make a huge difference in scaling your business efficiently.
The approval process requires company registration and meeting specific requirements, but we provide full support to guide you through every step. Start saving on your cloud infrastructure today and unlock the full potential of your business.