optomize index.ts
This commit is contained in:
parent
b280358b65
commit
367b0b8fe6
1 changed files with 33 additions and 25 deletions
34
src/index.ts
34
src/index.ts
|
@ -2,13 +2,14 @@
|
||||||
|
|
||||||
import compression from"compression";
|
import compression from"compression";
|
||||||
import express, { Request, Response }from"express";
|
import express, { Request, Response }from"express";
|
||||||
import fs from"node:fs";
|
import fs from"node:fs/promises";
|
||||||
import fetch from"node-fetch";
|
import fetch from"node-fetch";
|
||||||
import path from"node:path";
|
import path from"node:path";
|
||||||
import{ observe, uptime }from"./stats.js";
|
import{ observe, uptime }from"./stats.js";
|
||||||
import{ getApiUrls, inviteResponse }from"./utils.js";
|
import{ getApiUrls, inviteResponse }from"./utils.js";
|
||||||
import{ fileURLToPath }from"node:url";
|
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 __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = path.dirname(__filename);
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
|
@ -55,9 +56,9 @@ async function updateInstances(): Promise<void>{
|
||||||
|
|
||||||
updateInstances();
|
updateInstances();
|
||||||
|
|
||||||
app.use("/getupdates", (_req: Request, res: Response)=>{
|
app.use("/getupdates", async (_req: Request, res: Response)=>{
|
||||||
try{
|
try{
|
||||||
const stats = fs.statSync(path.join(__dirname, "webpage"));
|
const stats = await fs.stat(path.join(__dirname, "webpage"));
|
||||||
res.send(stats.mtimeMs.toString());
|
res.send(stats.mtimeMs.toString());
|
||||||
}catch(error){
|
}catch(error){
|
||||||
console.error("Error getting updates:", 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);
|
const filePath = path.join(__dirname, "webpage", req.path);
|
||||||
if(fs.existsSync(filePath)){
|
try{
|
||||||
|
await fs.access(filePath);
|
||||||
if(devmode){
|
if(devmode){
|
||||||
const filePath2 = path.join(__dirname, "../src/webpage", req.path);
|
const filePath2 = path.join(__dirname, "../src/webpage", req.path);
|
||||||
if(fs.existsSync(filePath2)){
|
try{
|
||||||
|
await fs.access(filePath2);
|
||||||
res.sendFile(filePath2);
|
res.sendFile(filePath2);
|
||||||
return;
|
return;
|
||||||
}
|
}catch{}
|
||||||
}
|
}
|
||||||
res.sendFile(filePath);
|
res.sendFile(filePath);
|
||||||
}else if(fs.existsSync(`${filePath}.html`)){
|
}catch{
|
||||||
|
try{
|
||||||
|
await fs.access(`${filePath}.html`);
|
||||||
if(devmode){
|
if(devmode){
|
||||||
const filePath2 = path.join(__dirname, "../src/webpage", req.path);
|
const filePath2 = path.join(__dirname, "../src/webpage", req.path);
|
||||||
if(fs.existsSync(filePath2+".html")){
|
try{
|
||||||
|
await fs.access(filePath2 + ".html");
|
||||||
res.sendFile(filePath2 + ".html");
|
res.sendFile(filePath2 + ".html");
|
||||||
return;
|
return;
|
||||||
}
|
}catch{}
|
||||||
}
|
}
|
||||||
res.sendFile(`${filePath}.html`);
|
res.sendFile(`${filePath}.html`);
|
||||||
}else{
|
}catch{
|
||||||
if(req.path.startsWith("/src/webpage")){
|
if(req.path.startsWith("/src/webpage")){
|
||||||
const filePath2 = path.join(__dirname, "..", req.path);
|
const filePath2 = path.join(__dirname, "..", req.path);
|
||||||
if(fs.existsSync(`${filePath2}`)){
|
try{
|
||||||
|
await fs.access(filePath2);
|
||||||
res.sendFile(filePath2);
|
res.sendFile(filePath2);
|
||||||
return;
|
return;
|
||||||
}
|
}catch{}
|
||||||
}
|
}
|
||||||
res.sendFile(path.join(__dirname, "webpage", "index.html"));
|
res.sendFile(path.join(__dirname, "webpage", "index.html"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const PORT = process.env.PORT || Number(process.argv[2]) || 8080;
|
const PORT = process.env.PORT || Number(process.argv[2]) || 8080;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue