Stop npm browser application from javascript - javascript

I am running a browser application through npm run dev and I would like to know if there is any way to close/kill this process from the javascript script which is running.

Call the global process.exit(1);
Documentation

Related

What is best practice for executing Node.js app in production on a WINDOWS machine?

I have a Node.js application that I require to run on Windows Server. For the development process, we execute the app simply through command-line or PowerShell using the standard command:
node index.js
What is the best practice and most performant way of running this application on Windows permanently? Or is running it through CMD or PowerShell as we already doing the best way? If so, is either PS or CMD better?
Running as a service is a no-brainer, but the question still remains even if the application is 'servicified', as launching a Node app as a service still requires the specification of a shell through which to execute, such as PowerShell or CMD. Is there another shell we should use? Or is there a way to not use a shell at all?
Please advise as it surprisingly doesn't seem like there's any standardized advice anywhere on the internet; which begs the question also: is simply no one out there running production Node.js apps on Windows perhaps..?
I suggest using a package such as pm2.
PM2 runs your process in the background. Meaning that you can exit your terminal, and your app will continue to run. Only way to turn it off is:
1: Shutting down your server
2: pm2 stop ProcessName

Bot shuts down when putty window is closed

I created a discord bot and am now attempting to run it off an Ubuntu Machine.
I installed the folders of the bot and NodeJs, here is what I used to install NodeJS:
sudo apt-get install -y nodejs
Then I used cd to select the directory, and started my bot using node index.js
The bot started, however when I went to close the putty and keep it running on the VPS the bot shutdown. Here is what the directory looks like.
I think the problem is that when you start the app in the putty window, that process is linked to the window and gets terminated when that is closed.
To avoid that you can use a host service like screen, tmux, nohup, bg and so on...
If you want to know which is the best, try looking at this question from the askUbuntu Stack Exchange.
The key concept is that you open a new window using the tmux command (or screen, ...), then run your bot like you always do. When you want to leave but keep the process runing, you can detach the session with a key combination, that changes from service to service.
If you want to access that window again, you can run a command that will "restore" your session, like
tmux list-sessions
tmux attach-session -t 0
The NodeJS instance is terminated when putty is closed. You need something to keep the instance alive. Try:
PM2: http://pm2.keymetrics.io/
or,
Forever: https://github.com/foreverjs/forever#readme
Recommended though is to run the node instance as a service that can reboot on startup. Try looking at this:
https://stackoverflow.com/a/29042953/7739392
The shell runs in the foreground. This means any scripts you start there will end once you end your session. A simple solution would be to run your script in the background by adding the & after the call:
node index.js &
A better solution would be to create a service you can ask the service daemon to run for you. However, adding the & should get you what you want for now.
I recommend using one of these two node modules - ForeverJS or PM2. I'll show you how to quickly get started with ForeverJS but PM2 would be very similar.
You can easily install ForeverJS by typing the following in your terminal:
$ npm install forever -g
You may need to use SUDO depending on your user's privileges to get this working properly. It is NOT recommended to use it in production due to the security risks.
Once installed CD to your projects file directory and like you typed 'node index.js' you will do something similar with ForeverJS.
$ forever start index.js
Now when you exit the terminal your NodeJS application will remain as a running process.

Meteor server crashes very frequently without any error

I am working with the Meteor application and I deployed the same over EC2 instance. It was working fine till a few days back.
But now the server process kill automatically without any error log or
console
I tried to get the error but unfortunately, as there are not any logs I am unable to find out why the server is crashing again and again.
I have a medium EC2 machine on which the application is running.
I am using nohup for running the application in the background.
Below is the command I used to start the server:-
nohup meteor --settings SETTINGS-PRODUCTION.JSON &
I am wondering to know about server crashes due to nohup or some other reason is there.
Please let me know how we can console uncaught exception in the meteor-like we do in express.
What should I use to auto restart the server if the process is killed
by any error or exception?
Any help would be much appreciated!
Thanks
Using nohup is quite a low-tech solution. Things like Phusion Passenger, PM2, or forever do a better job.
Also your docker container can be configured to automatically restart the process.
Even better is a tool called Meteor Up, which makes it really simple to deploy Meteor apps to EC2.
EASILY DEPLOY YOUR APP Meteor Up is a production quality Meteor app
deployment tool.
Install with one command:
$ npm install --global mup
http://meteor-up.com/

How to run a webapp in browser using localhost?

I have a program in node.js. I ran the code using "node main.js". It starts listening. When I try to open the link, "https://localhost:8080" it displays, "It works !
If you're seeing this page via a web browser,"
which is usually popped up after tomcat installation. how can i fix it and run my code?
You probably have a tomcat server running on the same port. Shut it down and run the node.js server only

Debuging expressjs server alongside electron

I've successfully running expressjs server alongside electron as described on here:
Run Node.js server file automatically after launching Electron App
The problem now, there is no output from command line related to server activity. I simply run
electron .
on project directory, then no other output related to the server.
Is there any way to get that server activity logged onto cli as normally I run with (like just) node server.js ??
Perhaps you can try using the node module concurrently. This will allow you to run two commands at once and is used commonly in development with electron.
Instead of getting the server and electron to run from within the same file, separate it into a server file and an electron file. For example, one of my main development techniques is this:
concurrently "npm run server" "npm run start-app"
Which runs my hot-reload server, that my electron app connects to in development mode.
By using concurrently you can see the output of each process as well.
A good example of this technique in practice is with the electron-react-boilerplate repository. If you're interesting in this style of development I recommend you clone that repository and give npm run dev a try, to see how their development process works (whether you're using React or not).

Categories