cli: optimize resource services

This commit is contained in:
Lxy 2022-08-04 10:48:18 +08:00 committed by osborn
parent ea6e079b4f
commit 8680362c75

View File

@ -1,51 +1,62 @@
import fs from "fs"
import path from "path"
import http from 'http';
import http from "http";
import { IncomingMessage, ServerResponse } from "http";
export async function createResServer() {
const port = 7778;
const server = http.createServer();
server.listen(port);
console.log(`Resource server is listening on port ${port} !`.green);
server.on('request', (request: IncomingMessage, response: ServerResponse) => {
console.log(`resource server is listening on port ${port} !`.green);
server.on("request", (request: IncomingMessage, response: ServerResponse) => {
const { url } = request;
if (url) {
try {
const pathArr = url.split("/");
const resName = pathArr[pathArr.length - 1];
const dirPath = path.join(process.cwd(), url.replace(`/${resName}`, ""));
const fileRealName = findResRealName(dirPath, resName);
const resourcePath = path.join(dirPath, `${fileRealName}`);
if (fs.existsSync(resourcePath)) {
const resourcePath = findFilePath(dirPath, resName);
if (resourcePath.length != 0) {
const data = fs.readFileSync(resourcePath);
response.setHeader("content-length", data.length);
response.setHeader("Content-Length", data.length);
response.write(data);
response.end();
} else {
response.setHeader("content-type", "text/html; charset=utf-8");
console.error(`Resources ${resName} do not exist !`.red)
response.end(`Resources ${resName} do not exist !`);
console.error(`resource ${resName} does not exist !`.red)
response.writeHead(404, { "Content-Type": "text/plain" });
response.end("Not Found");
}
} catch (error) {
response.setHeader("content-type", "text/html; charset=utf-8");
console.error(`Server internal error`.red)
response.end("Server internal error" + error);
response.writeHead(500, { "Content-Type": "text/plain" });
console.error("server internal error".red, error);
response.end("server internal error" + error);
}
} else {
response.setHeader("content-type", "text/html; charset=utf-8");
console.error(`url is undefined`.red)
response.setHeader("Content-Type", "text/html; charset=utf-8");
console.error("url is undefined".red);
response.end("url is undefined");
}
});
}
function findResRealName(dirPath: string, fileName: string) {
let fileFinalName = "";
const stats = fs.readdirSync(dirPath);
for (let stat of stats) {
if (stat.startsWith(`${fileName}.`) || stat === fileName) {
fileFinalName = stat;
return fileFinalName;
function findFilePath(dirPath: string, fileName: string) {
if (!fs.existsSync(dirPath)) {
return "";
}
const stats = fs.readdirSync(dirPath);
for (let item of stats) {
const itemPath = path.join(dirPath, item);
const stat = fs.statSync(itemPath);
if (stat.isDirectory()) {
const ret = findFilePath(itemPath, fileName) as string;
if (ret.length != 0) {
return ret;
}
} else {
if (item.startsWith(`${fileName}.`) || item === fileName) {
return path.join(dirPath, item);
}
}
}
}
return "";
}