A minimal Node.js HTTP server that reads HTML partials from disk using the core fs module.
This repo introduces:
fs.promises.readFile()(promise-based file I/O)- serving HTML from Node’s core
httpserver - “partials” (reusable HTML snippets)
Promise.all()to load multiple files
- Node.js installed (includes
node)
Verify:
node -vserver.js— HTTP server that loadspartials/header.htmlandpartials/footer.htmlpartials/header.html— reusable header snippetpartials/footer.html— reusable footer snippet
From the project folder:
npm run startYou should see:
Server running at http://127.0.0.1:3000/Then visit:
http://127.0.0.1:3000/
If the files load successfully, your terminal will log:
Partials loaded successfully.npm run watchNow edit partials/header.html or partials/footer.html, save, and refresh your browser to see the change.
Stop the server with:
Ctrl + C
const fs = require("fs").promises;const headerPath = __dirname + "/partials/header.html";__dirname points to the folder containing server.js, which keeps your paths stable even if you run Node from a different working directory.
Promise.all([fs.readFile(headerPath), fs.readFile(footerPath)])- Runs both reads at the same time
- Resolves when both are complete
- Rejects if either one fails
.
├── package.json
├── server.js
├── partials/
│ ├── header.html
│ └── footer.html
└── README.mdNode2Know-Learn-1.0 (see LICENSE).