我是node.js中的新手,我有一个简单的node.js项目,其中只有一个js文件( vpn.js ),它使用一个模块,而一个index.html使用vpn.js中的一个函数打开。我已经安装了这个软件包,需要的功能可以找到它的模块。我有一个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)
});发布于 2020-05-12 05:46:15
使用pkg npm软件包。这将为nodejs项目创建一个可执行文件。您可以为Windows、mac或linux创建可执行文件。
使用以下命令全局安装pkg
npm install -g pkg安装后,使用:pkg app.js[entry file to your project]创建可执行文件。
有关pkg的更多信息,请查看pkg
发布于 2020-05-12 05:22:02
您可以帮助设置永久自动运行节点服务器。以下是一些步骤
您还可以考虑使用upstart实用程序。它将允许您像服务一样启动、停止和重新启动节点应用程序。Upstart可以配置为在应用程序崩溃时自动重新启动。
安装upstart:
sudo apt-get install upstart为应用程序创建一个简单的脚本,如下所示:
#!upstart
description "my app"
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
env NODE_ENV=production
exec node /somepath/myapp/app.js >> /var/log/myapp.log 2>&1然后将脚本文件(myapp.conf)复制到/etc/init,并确保其标记为可执行文件。然后可以使用以下命令管理您的应用程序:
sudo start myapp
sudo stop myapp
sudo restart myapphttps://stackoverflow.com/questions/61744329
复制相似问题