Files
mqtt-ingest/app/src/http/server.ts
2026-07-05 16:09:32 +05:00

31 lines
928 B
TypeScript

import { readFile } from 'node:fs/promises'
import { createServer } from 'node:http'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { log } from '../helpers/log.js'
const httpPort = Number(process.env.HTTP_PORT)
const assetsDir = join(dirname(fileURLToPath(import.meta.url)), '../../assets')
const indexPath = join(assetsDir, 'index.html')
createServer(async (req, res) => {
const path = req.url?.split('?')[0]
if (path === '/' || path === '/index.html') {
try {
const html = await readFile(indexPath, 'utf8')
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' })
res.end(html)
} catch (err) {
console.error('http.read_index_error', err)
res.writeHead(500).end('Internal Server Error')
}
return
}
res.writeHead(404).end('Not Found')
}).listen(httpPort, () => {
log('http.server_started', { port: httpPort })
})