gpt4 book ai didi

javascript - 如何在没有终端命令的情况下运行 Node js 应用程序

转载 作者:行者123 更新时间:2023-12-04 13:14:25 28 4
gpt4 key购买 nike

我是 node.js 的新手,我有一个简单的 node.js 项目,只有一个 js 文件 (vpn.js),它使用一个模块和一个使用 vpn.js 中的函数打开的 index.html。我已经安装了这个包并且 require 函数可以找到它的模块。我有一个 vpn.js 文件和一个 index.html(在 index.html 中我只有一个带有 src 的视频标签。)。现在我的问题是我应该总是用终端运行我的代码吗?我应该如何主持这个项目?基本上没有客户端可以在网络上运行终端命令。 (注意:我使用的是 Windows 而不是 Linux)这是我的 js 文件的代码:

const openvpnmanager = require('node-openvpn');

const opts = {
host: '192.168.1.7', // normally '127.0.0.1', will default to if undefined
port: 8080, //port openvpn management console
timeout: 1500, //timeout for connection - optional, will default to 1500ms if undefined
logpath: 'log.txt' //optional write openvpn console output to file, can be relative path or absolute
};
const auth = {
user: 'vpnUserName',
pass: 'vpnPassword',
};
const openvpn = openvpnmanager.connect(opts)

// will be emited on successful interfacing with openvpn instance
openvpn.on('connected', () => {
//openvpnmanager.authorize(auth);
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('index.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(5500);
});

// emits console output of openvpn instance as a string
openvpn.on('console-output', output => {
console.log(output);

});

// emits console output of openvpn state as a array
openvpn.on('state-change', state => {
console.log(state)
});

// emits console output of openvpn state as a string
openvpn.on('error', error => {
console.log(error)
});

最佳答案

使用 pkg npm 包。这将为您的 nodejs 项目创建一个可执行文件。您可以为 Windows 或 mac 或 linux 创建可执行文件。

使用以下命令全局安装 pkg

npm install -g pkg

安装后,使用:pkg app.js[项目的入口文件] 以创建可执行文件。

有关 pkg 的更多信息,请查看 pkg

关于javascript - 如何在没有终端命令的情况下运行 Node js 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61744329/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com