import http from "node:http"
class Express {
constructor() {
this.routes = {}
}
get(path, handler) {
this.routes[path] = {
method: "GET",
handler: handler
}
}
listen(port = 8080) {
http.createServer((request, response) => {
const path = request.url
if (this.routes[path]) {
this.routes[path].handler(new Express_Request(), new Express_Response(response))
} else {
new Express_Response(response).json({ERROR: "404 NOT FOUND"}, 404)
}
response.end()
})
.listen(port)
console.log(`server listen on port ${port}`)
}
}
class Express_Request {}
class Express_Response {
constructor(http_builtinResponse) {
this._response = http_builtinResponse
}
json(object, status = 200) {
this._response.writeHead(status, {
"Content-Type": "application/json",
"Content-Length": JSON.stringify(object).length
})
this._response.write(JSON.stringify(object))
this._response.end()
}
}
export {Express}