change: removed .html in url
All checks were successful
Deploy on push / deploy (push) Has been skipped
All checks were successful
Deploy on push / deploy (push) Has been skipped
This commit is contained in:
@@ -94,6 +94,15 @@ function resolveRequestPath(pathname) {
|
||||
return path.join(absolutePath, "index.html");
|
||||
}
|
||||
|
||||
if (
|
||||
path.extname(absolutePath) === "" &&
|
||||
!fs.existsSync(absolutePath) &&
|
||||
fs.existsSync(`${absolutePath}.html`) &&
|
||||
fs.statSync(`${absolutePath}.html`).isFile()
|
||||
) {
|
||||
return `${absolutePath}.html`;
|
||||
}
|
||||
|
||||
return absolutePath;
|
||||
}
|
||||
|
||||
@@ -113,6 +122,14 @@ function sendText(response, statusCode, body) {
|
||||
response.end(body);
|
||||
}
|
||||
|
||||
function sendRedirect(response, location) {
|
||||
response.writeHead(301, {
|
||||
Location: location,
|
||||
"Cache-Control": "no-store",
|
||||
});
|
||||
response.end();
|
||||
}
|
||||
|
||||
function parseJsonBody(request) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const chunks = [];
|
||||
@@ -194,14 +211,29 @@ async function handleApiRequest(request, response, pathname, options) {
|
||||
function createServer(options) {
|
||||
return http.createServer(async (request, response) => {
|
||||
let pathname;
|
||||
let search;
|
||||
|
||||
try {
|
||||
pathname = decodeURIComponent(new URL(request.url || "/", "http://localhost").pathname);
|
||||
const requestUrl = new URL(request.url || "/", "http://localhost");
|
||||
pathname = decodeURIComponent(requestUrl.pathname);
|
||||
search = requestUrl.search;
|
||||
} catch (error) {
|
||||
sendText(response, 400, "Bad request");
|
||||
return;
|
||||
}
|
||||
|
||||
if (request.method === "GET" || request.method === "HEAD") {
|
||||
if (pathname === "/index.html") {
|
||||
sendRedirect(response, `/${search}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (pathname.endsWith(".html") && pathname !== "/index.html") {
|
||||
sendRedirect(response, `${pathname.slice(0, -5)}${search}`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (await handleApiRequest(request, response, pathname, options)) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user