javascript - How to run node js application without terminal command - Stack Overflow

I'm so new in node.js and I have a simple node.js project with only one js file (vpn.js) which use

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. I have installed this package and the require function can find its module. I have a vpn.js file and an index.html (in index.html I only have a video tag with a src.). Now my question is should I always run my code with terminal? how should I host this project? Basically no clients can run terminal mands on web. (note: I'm using Windows not Linux) this is the code of my js file:

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)
});

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. I have installed this package and the require function can find its module. I have a vpn.js file and an index.html (in index.html I only have a video tag with a src.). Now my question is should I always run my code with terminal? how should I host this project? Basically no clients can run terminal mands on web. (note: I'm using Windows not Linux) this is the code of my js file:

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)
});
Share Improve this question edited May 12, 2020 at 5:28 elahe karami asked May 12, 2020 at 5:17 elahe karamielahe karami 6936 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Use pkg npm package. This will create an executable file for your nodejs project. You can create executable file for Windows or mac or linux.

Install pkg globally using following mand

npm install -g pkg

After installing it, use: pkg app.js[entry file to your project] to create executable file.

For more info about pkg, look into pkg

you can done it help of set autorun the node server forever. here is some step

You may also want to consider using the upstart utility. It will allow you to start, stop and restart you node application like a service. Upstart can configured to automatically restart your application if it crashes.

Install upstart:

sudo apt-get install upstart

Create a simple script for your application that will look something like:

#!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

Then copy the script file (myapp.conf) to /etc/init and make sure its marked as executable. Your application can then be managed using the following mands:

sudo start myapp
sudo stop myapp
sudo restart myapp

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744136946a4560102.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信