使用node.js和python制作Windows服务

问题描述

我想将此节点服务器作为Windows服务,如果只是一个print(“ hello world”),则运行python脚本,但我希望它创建目录或保存图像,在这种情况下,它不会只有当我使用NSSM.exe创建服务,但是当我运行节点服务器(node index.js)时,它才能正常工作,而当服务器成为Windows服务后,它可以在给定端口上运行并侦听,但没有完成工作。 这是代码:

-node.js代码


const http = require("http");
const fs = require("fs");
const spawn = require("cross-spawn");
const PORT = 3002;
const server = http.createServer(function (req,res) {
  if (req.url === "/?test=hidden") {
    const spawn = require("child_process").spawn;
    // this doesn't work if i just pass "python" on the spawn
    const process = spawn(
      "C:\\Users\\adnan\\AppData\\Local\\Programs\\Python\\Python38\\python.exe",["C:\\CustomServices\\Who\\main.py"]
    );
    process.stdout.on("data",(data) => {
      console.log(data);
    });
    setTimeout(() => {
      res.write("Sent Successfully ! " + data);
      res.end();
    },10800);
  } else {
    res.writeHead(200,{ "Content-Type": "text/html" });
    fs.readFile("C:\\CustomServices\\Who\\index.html",function (error,data) {
      if (error) {
        res.writeHead(404);
        res.write("Error:FileNotFound");
      } else {
        res.write(data);
      }
      res.end();
    });
  }
});
server.listen(PORT,function (error) {
  if (error) {
    console.log("something went wrong",error);
  } else {
    console.log("server is listening on http://localhost:" + PORT);
  }
});

python代码:

import pyautogui as py


def gotcha():
    py.screenshot().save("C:\\CustomServices\\Who\\user\\user_2.png")

gotcha()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)