I don't understand why is not working. The compiler is asking for the async in the await function in router, and I have the async in router exports.
Server:
const http = require("http");
const router = require("./router");
module.exports = http.createServer((request, response) => {
console.log(request.url);
try {
const content = await router(request, response);
return response.end(content);
} catch (error) {
response.writeHead(404,{
'Content-Type': 'text/html',
});
response.end(`<h1>${error.message}</h1>`);
}
});
Router:
const { parse } = require("url");
const indexAction = require("./actions/index");
const routes = {
"/": indexAction,
};
module.exports = async (request, response) => {
const parseUrl = parse(request.url, true);
const route = parseUrl.pathname;
if (routes[route]) {
return await routes[route](request, response, parseUrl.query);
}
throw new Error("404 Not Found");
};