elahe karami
elahe karami

Reputation: 693

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

Upvotes: 3

Views: 8878

Answers (2)

smnth90
smnth90

Reputation: 828

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 command

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

Upvotes: 4

Harsukh Makwana
Harsukh Makwana

Reputation: 4494

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 commands:

sudo start myapp
sudo stop myapp
sudo restart myapp

Upvotes: 1

Related Questions