This project was a hands-on learning experience to build a full-stack web application from the ground up, with a specific focus on creating a robust backend using only the core Node.js APIs, without any external frameworks like Express.
This document covers the backend architecture and the key concepts I learned and implemented.
This project was my deep dive into the fundamentals of how Node.js works under the hood. By avoiding frameworks, I was forced to understand and implement the core logic that powers modern web applications.
The entire application is built on top of the native http module.
- Key Skills:
- Initializing a server with
http.createServer(). - Understanding the request (
req) and response (res) objects. - Handling the asynchronous nature of the request/response cycle.
- Making the server listen for connections on a specific port.
- Initializing a server with
From the start, I structured the project into logical, reusable modules using modern ES Module syntax (import/export).
- Key Skills:
- Setting
"type": "module"inpackage.json. - Separating concerns into
utils,handlers, andpublicdirectories. - Using
import.meta.dirnameto reliably create absolute paths for file system operations, which is crucial in ES Modules.
- Setting
Instead of using a pre-built router, I created my own logic to direct incoming requests based on their URL and HTTP method.
- Key Skills:
- Inspecting
req.urlandreq.methodto differentiate between API calls and file requests. - Handling different HTTP verbs, specifically
GETfor data retrieval andPOSTfor data submission. - Creating API endpoints like
/api/live-priceand/api/invest. - Sending a
405 Method Not Allowedstatus for incorrect HTTP methods.
- Inspecting
I learned how web servers serve static assets like HTML, CSS, and JavaScript by building my own static file server.
- Key Skills:
- Using
fs/promisesto read file contents asynchronously. - Using the
pathmodule to safely join path segments and prevent security issues. - Creating a utility to determine the correct
Content-Typeheader based on a file's extension (e.g.,text/htmlvs.text/css). - Implementing a custom 404 "Not Found" page for requests that don't match any file.
- Using
One of the most valuable learning experiences was building a body parser from scratch to handle incoming POST data.
- Key Skills:
- Reading an incoming request stream chunk by chunk using the
for await...ofloop. - Assembling the chunks into a complete string.
- Parsing the string as JSON using
JSON.parse(). - Wrapping the parsing logic in a
try...catchblock to handle malformed JSON and prevent the server from crashing.
- Reading an incoming request stream chunk by chunk using the
To provide live gold prices, I implemented a Server-Sent Events endpoint. This was a great introduction to pushing data from the server to the client in real-time.
- Key Skills:
- Setting the required HTTP headers for an SSE connection (
Content-Type: text/event-stream,Cache-Control: no-cache,Connection: keep-alive). - Using
setIntervalon the server to periodically send data. - Formatting the data payload correctly with
res.write('data: ...\n\n')so the client'sEventSourceAPI can parse it.
- Setting the required HTTP headers for an SSE connection (
I implemented the core application logic and learned to persist data on the server's file system.
- Key Skills:
- Using the
cryptomodule to generate a unique transaction ID withcrypto.randomUUID(). - Using
fs/promises.appendFileto write transaction details to ainvestments.logfile, ensuring a persistent record of all investments. - Implementing the core business logic: calculating the amount of gold purchased based on the investment amount and the current market price.
- Using the
This project solidified my understanding of the foundational layers of backend web development in the Node.js ecosystem.