Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.
I needed to serve an HTML page from a Node.js server, and this is the simplest code that does the trick:
const http = require('http')
const fs = require('fs')
const server = http.createServer((req, res) => {
res.writeHead(200, { 'content-type': 'text/html' })
fs.createReadStream('index.html').pipe(res)
})
server.listen(process.env.PORT || 3000)
No dependencies needed.
Add this to an app.js file, then create an index.html page, and run node app.js
Notice that the above code does not provide support for serving static assets, it only serves the index.html page.