const http = require('http'); const fs = require('fs'); const path = require('path'); const { parse } = require('querystring'); const lib= require('./lib.js'); //导入快速'S文件 lib.fmkdir("logs") const server = http.createServer((req, res) => { const { url, method } = req; const modid = url.slice(1); // 移除开头的 '/' const logFilePath = `logs/${getCurrentDate()}.log`; // 记录请求日志 const logMessage = `【${getCurrentTime()}】${req.connection.remoteAddress} 访问,状态码:`; appendToLogFile(logFilePath, logMessage); if (method === 'GET' && modid) { const filePath = path.join(__dirname, 'mod', `${modid}.json`); fs.readFile(filePath, (err, data) => { if (err) { res.writeHead(404, { 'Content-Type': 'application/json' }); const response = JSON.stringify({ modid, client: 'unknown' }); res.end(response); appendToLogFile(logFilePath, '404'); } else { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(data); appendToLogFile(logFilePath, '200'); } }); } else { res.writeHead(400, { 'Content-Type': 'text/plain' }); res.end('Bad Request'); appendToLogFile(logFilePath, '400'); } }); const port = 3001; server.listen(port, () => { console.log(`服务器正在监听端口 http://localhost:${port}`); }); function appendToLogFile(filePath, message) { fs.appendFile(filePath, message + '\n', err => { if (err) { console.error('追加日志文件时出错:', err); } }); } function getCurrentDate() { const today = new Date(); const year = today.getFullYear(); const month = String(today.getMonth() + 1).padStart(2, '0'); const day = String(today.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; } function getCurrentTime() { const now = new Date(); return now.toLocaleTimeString(); }