optomize index.ts

This commit is contained in:
Scott Gould 2024-09-25 14:05:17 -04:00
parent b280358b65
commit 367b0b8fe6
No known key found for this signature in database

View file

@ -2,13 +2,14 @@
import compression from"compression";
import express, { Request, Response }from"express";
import fs from"node:fs";
import fs from"node:fs/promises";
import fetch from"node-fetch";
import path from"node:path";
import{ observe, uptime }from"./stats.js";
import{ getApiUrls, inviteResponse }from"./utils.js";
import{ fileURLToPath }from"node:url";
const devmode = (process.env.NODE_ENV || 'development')==='development';
import process from"node:process";
const devmode = (process.env.NODE_ENV || "development") === "development";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@ -55,9 +56,9 @@ async function updateInstances(): Promise<void>{
updateInstances();
app.use("/getupdates", (_req: Request, res: Response)=>{
app.use("/getupdates", async (_req: Request, res: Response)=>{
try{
const stats = fs.statSync(path.join(__dirname, "webpage"));
const stats = await fs.stat(path.join(__dirname, "webpage"));
res.send(stats.mtimeMs.toString());
}catch(error){
console.error("Error getting updates:", error);
@ -103,34 +104,41 @@ app.use("/", async (req: Request, res: Response)=>{
}
const filePath = path.join(__dirname, "webpage", req.path);
if(fs.existsSync(filePath)){
try{
await fs.access(filePath);
if(devmode){
const filePath2 = path.join(__dirname, "../src/webpage", req.path);
if(fs.existsSync(filePath2)){
try{
await fs.access(filePath2);
res.sendFile(filePath2);
return;
}
}catch{}
}
res.sendFile(filePath);
}else if(fs.existsSync(`${filePath}.html`)){
}catch{
try{
await fs.access(`${filePath}.html`);
if(devmode){
const filePath2 = path.join(__dirname, "../src/webpage", req.path);
if(fs.existsSync(filePath2+".html")){
try{
await fs.access(filePath2 + ".html");
res.sendFile(filePath2 + ".html");
return;
}
}catch{}
}
res.sendFile(`${filePath}.html`);
}else{
}catch{
if(req.path.startsWith("/src/webpage")){
const filePath2 = path.join(__dirname, "..", req.path);
if(fs.existsSync(`${filePath2}`)){
try{
await fs.access(filePath2);
res.sendFile(filePath2);
return;
}
}catch{}
}
res.sendFile(path.join(__dirname, "webpage", "index.html"));
}
}
});
const PORT = process.env.PORT || Number(process.argv[2]) || 8080;