How To Set Up A Basic HTTP Server In Node.js

Learn how to set up a basic HTTP server in Node.js with this hands-on tutorial featuring an interactive, editable code demo you can run in your browser.

MIME Types

Node.js lacks built in mime type resolution, and sometimes the correct mimetype is needed. Find some functions to help with this purpose.
https://github.com/javascript-2020/libs/blob/main/js/string/getmime/getmime.js
https://github.com/javascript-2020/libs/blob/main/js/string/getmime/getmime-simple.js

X509 Certificates ( HTTPS )

Theres a fully comprehensive x509 certificate suite on the ext-code.com website. HTTPS certificates can be created
generate https certificates ext-code.com

http-server-simple-example.js

This example demonstrates a simple server, it produces cors responses, has a built-in /hello page and has a built-in favicon.

It serves files local to the base directory where the server was run and produces a 400 badrequest when the requested url maps to a directory outside the directory where the server is located such as '../test.txt'.

It produces a 404 Not Found should the request url not exists.

The mime function will need to built up if it were required.

The server is typically accessed via
https://localhost:3001/hello

http-server-minimal.js

This example demonstrates a minimal http server. Sometimes simple is best.

The server is typically accessed via http://localhost:3002/

http-server-login-example.js

This example demonstrates a simple login server, it demonstrates the use of cookies to maintain state.

The server is typically accessed via
http://localhost:3003/home.html

Node.js | HTTP nodejs.org

This module, containing both a client and server, can be imported via require('node:http') (CommonJS) or import * as http from 'node:http' (ES module).

The HTTP interfaces in Node.js are designed to support many features of the protocol which have been traditionally difficult to use. In particular, large, possibly chunk-encoded, messages. The interface is careful to never buffer entire requests or responses, so the user is able to stream data.