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