Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Building a Basic HTTP Server with Node.js

Tech 1

To verify Node.js installlation, run the version check command:

node --version

A Node.js application consists of three core components:

  1. Module imports using require()
  2. Server creation to handle client requests
  3. Request/resopnse handlign logic

Here's how to create a simple HTTP server:

const server = require('http');

server.createServer((req, res) => {
    res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
    res.end('Node.js server response\n');
}).listen(8080);

console.log('Server active on port 8080');

For HTML responses:

const http = require('http');

http.createServer((request, reply) => {
    reply.writeHead(200, {'Content-Type': 'text/html'});
    reply.write('<meta charset="UTF-8">');
    reply.end('<h1>Node.js Server Response</h1>');
}).listen(3000);

Key observations:

  • Content-Type header determines response format (text/plain vs text/html)
  • Character encoding can be specified in headers or HTML meta tags
  • Avoid well-known ports (80, 23) for development servers
Tags: Node.js

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.