1.8.8 • Published 11 months ago

artificial-lime-eagle v1.8.8

Weekly downloads
-
License
-
Repository
-
Last release
11 months ago

Simple Node.js HTTP Server

A simple and extensible Node.js HTTP server with routing, static file serving, and request parsing functionality. This project demonstrates how to build a basic HTTP server without using any additional packages.

Table of Contents

Installation

To get started, clone this repository and navigate to the project directory:

cd simple-node-http-server

Usage

Run the server using Node.js:

node index.js

The server will start on http://localhost:3000.

API

GET /

Returns a simple welcome message.

Request

GET / HTTP/1.1
Host: localhost:3000

Response

HTTP/1.1 200 OK
Content-Type: text/plain

Welcome to the Home Page

GET /about

Returns a message about the page.

Request

GET /about HTTP/1.1
Host: localhost:3000

Response

HTTP/1.1 200 OK
Content-Type: text/plain

This is the About Page

GET /data

Returns the query parameters as a JSON object.

Request

GET /data?name=John&age=30 HTTP/1.1
Host: localhost:3000

Response

HTTP/1.1 200 OK
Content-Type: application/json

{
  "name": "John",
  "age": "30"
}

POST /submit

Accepts and returns the POSTed data as a JSON object.

Request

POST /submit HTTP/1.1
Host: localhost:3000
Content-Type: application/x-www-form-urlencoded

name=John&age=30

Response

HTTP/1.1 200 OK
Content-Type: application/json

{
  "message": "Data received",
  "data": "name=John&age=30"
}

Static Files

Serve static files from the static directory. For example, accessing /static/example.html will serve the example.html file from the static directory.

Request

GET /static/example.html HTTP/1.1
Host: localhost:3000

Response

HTTP/1.1 200 OK
Content-Type: text/html

<!-- Contents of example.html -->

Contributing

Contributions are welcome! Please fork the repository and submit a pull request for any improvements or bug fixes.

  1. Fork the repository
  2. Create a new branch (git checkout -b feature/your-feature)
  3. Commit your changes (git commit -am 'Add your feature)
  4. Push to the branch (git push origin feature/your-feature)
  5. Create a new Pull Request