繁体   English   中英

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

[英]How to run node js application without terminal command

I'm so new in node.js and I have a simple node.js project with only one js file (vpn.js) which use a module and an index.html which opens using a function in vpn.js. 我已经安装了这个 package 并且需要 function 可以找到它的模块。 我有一个 vpn.js 文件和一个 index.html(在 index.html 中,我只有一个带有 src 的视频标签)。 现在我的问题是我应该总是用终端运行我的代码吗? 我应该如何主持这个项目? 基本上没有客户端可以在 web 上运行终端命令。 (注意:我使用的是 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)
});

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

使用以下命令全局安装 pkg

npm install -g pkg

安装后,使用: pkg app.js[entry file to your project]创建可执行文件。

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

你可以帮助永远设置自动运行节点服务器。 这是一些步骤

您可能还需要考虑使用 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 myapp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM