jgkp9p1macbvfqzu / index.js
connorlixyz's picture
Update index.js
b12a0f9 verified
const axios = require('axios');
const fs = require('fs');
const cron = require('node-cron');
const http = require('http');
const path = require('path');
const port = process.env.PORT || 7860;
// 定义要访问的网页URL数组
const urls = [
// huggingface
'https://connorlixyz-jgkp9p1macbvfqzu.hf.space', // huggingface-本项目
// Goorm
//'https://75b4f056-6316-4113-8079-5465cacbbc7d.e1-eu-north-azure.choreoapps.dev', // Goorm-linshi
'https://798f9c74-90e5-496e-b0bf-fe010324b0e7.e1-eu-north-azure.choreoapps.dev', // MC-servers-py
// US4
'https://20c2aa79-0795-4b71-bad9-8a8d05ed3e92.e1-eu-north-azure.choreoapps.dev', // MC-servers-py
// 添加更多的URL
];
// 创建日志文件
//const logFile = 'visit-log.txt';
// 访问网页并将结果写入日志
async function scrapeAndLog(url) {
try {
const response = await axios.get(url, {timeout: 3000});
const timestamp = new Date().toISOString();
const logMessage = `${timestamp}: Web visited Successfully ${url}\n`;
// 将访问结果写入日志文件
// fs.appendFileSync(logFile, logMessage);
console.log(logMessage);
} catch (error) {
const timestamp = new Date().toISOString();
const errorMessage = `${timestamp}: Web visited Error ${url}: ${error.message}\n`;
// 将错误信息写入日志文件
// fs.appendFileSync(logFile, errorMessage);
console.error(errorMessage);
}
}
// 使用cron来安排定期任务
cron.schedule('*/1 * * * *', () => {
console.log('Running webpage access...');
// 循环访问每个URL
urls.forEach((url) => {
scrapeAndLog(url);
});
});
const server = http.createServer((req, res) => {
if (req.url === '/') {
const filePath = path.join(__dirname, 'index.html');
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(500);
res.end('Error loading index.html');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
}
});
} else {
res.writeHead(404);
res.end('Not Found');
}
});
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});