Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Building a Basic HTTP Server with Node.js

Tech Apr 23 9

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

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...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

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