component-demo/index.js
Joshua Bemenderfer 05c575bb26 Initial commit.
2020-09-22 20:42:46 -04:00

52 lines
1.1 KiB
JavaScript

import http from 'http'
import path from 'path'
import { page } from './pages/Page.js'
import { HomePage } from './pages/Home.js'
import { AboutPage } from './pages/About.js'
const SERVER_PORT = 8080
function main() {
const server = http.createServer(async (req, res) => {
const url = new URL(req.url, `https://noop.local`)
const params = url.searchParams
// Ignore requests to files with extensions.
if (path.extname(url.pathname)) {
res.writeHead(404)
res.end()
return
}
const title = params.get('title') || `${url.pathname}?title=YOUR_TITLE`
const pageMap = {
'/': HomePage,
'/about': AboutPage
}
const selectedPage = pageMap[url.pathname] || pageMap['/']
try {
const output = await page(selectedPage, {
path: url.pathname,
title
})
res.write(output)
} catch (e) {
console.error(e)
res.writeHead(500)
}
res.end()
})
server.listen(SERVER_PORT, () => {
console.info(`Now listening for requests on ${SERVER_PORT}.`)
})
}
main()