Skip to content

Instantly share code, notes, and snippets.

@maohais
Created March 2, 2025 12:51
Show Gist options
  • Save maohais/e4205417639cba0231d5696e3a53d7f8 to your computer and use it in GitHub Desktop.
Save maohais/e4205417639cba0231d5696e3a53d7f8 to your computer and use it in GitHub Desktop.
const express = require("express");
const app = express();
const port = 3000;
var exec = require("child_process").exec;
const { createProxyMiddleware } = require("http-proxy-middleware");
const path = require('path');
const fs = require('fs');
const currentDir = __dirname;
process.chdir(currentDir);
app.use('/', createProxyMiddleware({
target: 'http://127.0.0.1:PORT',
changeOrigin: true,
ws: true,
onError: (err, req, res) => {
res.writeHead(500, {
'Content-Type': 'text/plain',
});
res.end('Please wait for a while and try to refresh the page.');
},
}));
function keep_web_alive() {
exec("pgrep -laf server.js", function (err, stdout, stderr) {
if (stdout.includes("server.js")) {
console.log("web 正在运行");
} else {
exec(
"node server.js",
function (err, stdout, stderr) {
if (err) {
console.log("保活-调起web-命令行执行错误:" + err);
} else {
console.log("保活-调起web-命令行执行成功!");
}
}
);
}
});
}
setInterval(keep_web_alive, 10 * 1000);
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment