How to start a .js file after it is stopped - javascript

i was looking to make a file / script that will restart a .js file after it has been stopped. after it is stopped, i want the new file to enter a command somewhere along the lines of this: nodejs server.jsin my VPS window .
so what i am getting at is like this:
1) i have my server.js up and running on my VPS with the command; nodejs server.js
2) it will automatically stop (I have this part down)
3) i want to have another script that restarts the node.js server after it has been stopped.
sorry if i am jumbling my words, or asking a silly question, just not sure how to go about this!
Thanks!

install module node-cmd in project directory
suppose you have too file server.js and app.js which listen on two different port
keep both file in same directory(not mandatory but to run with out path confusion )
server.js
const app = require('express')();
app.get('/',function(req,res){
res.send("running");
});
var cmd=require('node-cmd');
cmd.run('node server.js');
app.listen(8080);
console.log('listening on port 8080');
app.js
const app = require('express')();
app.get('/',function(req,res){
res.send("support file which will start server.js");
});
var cmd=require('node-cmd');
cmd.run('node server.js');
app.listen(8090);
console.log('listening on port 8090');
call app.js when you want to run server.js

nodemon usage is for when you change your server code or any code of your project then automaticaly restart server so when the app crashed it does not restart your server.js until you dont have a change in your codes so you need to use a package like forever or pm2 that start server continuously forever and if your app crashs then they will restart your server.js just after a few miliseconds here is the documentation :
https://github.com/foreverjs/forever
http://pm2.keymetrics.io/

Related

How to deploy a Vue.js application on Node.js server

I have a dist folder containing CSS, fonts, JS folder and an index.html file minimized for Vue.js, ready to deploy and use. I want to use Node.js to run this application. How can I set this up to just run npm run server and have it deployed on a specific port requested? Not sure how to structure this or if I need to build it in a specific way to run this Vue app. Any help would be greatly appreciated.
Since Vue is only a frontend library, the easiest way to host it and do things like serve up assets is to create a simple Express friendly script that you can use to start a mini-web server. Read up quickly on Express if you haven’t already. After that, add express:
npm install express --save
Now add a server.js file to your project’s root directory :
// server.js
var express = require('express');
var path = require('path');
var serveStatic = require('serve-static');
app = express();
app.use(serveStatic(__dirname + "/dist"));
var port = process.env.PORT || 5000;
var hostname = '127.0.0.1';
app.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
after that you could run :
node server
and your project will be served at the given host and port
Assuming that you have already the dist directory, if you don't have it run :
npm run build
in order to generate it

Is there a way to restart node server in runtime?

I am trying to define an endpoint in my express server that whenever this end point is called, the server restarts automatically in runtime.
for example, using express my server would look something like this ...
var express = require('express')
var app = express();
app.post('/restart', (req,res)=>{
//restart or create a new instance of the server
// then reply
res.json({
'message': 'server restarted successfully'
})
})
// =======================
// start the server ======
// =======================
var port = process.env.PORT || 8000;
app.listen(port);
console.log('server running at http://localhost:' + port);
NOTE: Although I am using expressJS, I am open to other solutions like HAPI for example.
Thanks in advance
The only way I know of how to restart a node instance is in the CLI level via npm forever or the pm2, but this is for deployment level xP.
You would need npm module forever to be globally installed on your system and Shelljs as a dependency. Initially start your server as forever start {Path to server.js}. Then you can do
var express = require('express')
var shell = require('shelljs')
var app = express();
app.post('/restart', (req,res)=>{
//restart or create a new instance of the server
shell.exec('forever restart {Path to server.js}');
// then reply
res.json({
'message': 'server restarted successfully'
})
})
// =======================
// start the server ======
// =======================
var port = process.env.PORT || 8000;
app.listen(port);
console.log('server running at http://localhost:' + port);
Also see that you will not get a response as the server would have restarted. You would just get a refused to connect.
You can use PM2 to start, stop your server using simple commands.
Starting an application in production mode is as easy as:
pm2 start app.js
Stop all apps
pm2 stop all
Restart all apps
pm2 restart all
I hope this will work for you.
HTH Thanks!
Since express uses the HTTP from Node, you might initialize the Express server by yourself with the Node HTTP functions, noted here.
Once you have the server started, you might close it and restart it as you wish, as mentioned here.
Just you have to be careful with the already opened connections, as calling the HTTP instance for close will leave the already opened connection(s) still open. More information about closing them all; can be found here.

Set up proxy server for create react app

I have started a react application using create-react-app and ran the npm run eject script to gain access to all files. I afterwards installed express and created server.js file that sits on same level as package.json file
these are server.js file contents:
const express = require('express');
const app = express;
app.set('port', 3031);
if(process.env.NODE_ENV === 'production') {
app.use(express.static('build'));
}
app.listen(app.get('port'), () => {
console.log(`Server started at: http://localhost:${app.get('port')}/`);
})
Nothing crazy here, just setting up for future api proxies where I need to use secrets and as I don't want to expose my api.
after this I added a "proxy": "http://localhost:3001/" to my package.json file. I am now stuck as I need to figure out how to start my server correctly and use this server.js file in development mode and afterwards in production.
Ideally It would also be good if we could use more that one proxy i.e. /api and /api2
You didn't have to eject to run your server.js. You can just run it with node server.js together with create-react-app.
You can still do npm start even after ejecting to start your dev server.
To run /api1 and /api2, you just have to handle it in your server.js file and it should work just fine. You need to match the port in your server.js and the one in proxy settings inside package.json - in this case, it should be "proxy": "http://localhost:3031"

Getting web application production ready (angularjs/nodejs)

My web application consists of angularjs on front end side and nodejs server listening to client requests. This is my folder structure:Folder Structure
UX contains client side code and IT contains server side code. I am using gulp to watch over development changes and for packaging (you can see the dist folder in UX). I use two terminals to launch this web application locally. From one terminal, I use gulp serve (UX folder) to start a static UI server which monitors the changes as I make to UI and reflect back the changes on the browser immediately. From the second terminal, I start a node server2.js server.
The UX/src/app folder has a config file where I specify server ip address and app.js uses this info to connect to server (currently).
Now, I want to deploy this app over cloud. On the cloud, I have to specify a node it/server2.js as a starting point in its config file. Hence, I want the corresponding web link should point to index.html in UX/src/app folder.
Hence, I need some advice on how to integrate my client side app.js file in the server2.js file on server side.
I am an amateur.
Thanks a lot!
I added this code to my server2.js file:
var express = require("express");
var app = express();
app.use(express.static("ux/dist/"));
app.get("/", function(req, res, next){
res.sendFile('index.html');
});
Currently, it is invoking index.html page from UX folder. But, I am not sure whether I have done it the ideal way. Need help on this.

How to setup simple static server where path '/' points to index.html within dirrectory?

There might be a very simple solution to this question, but I am not able to find answer online and due to my practice with node I can't figure it out either.
I'm trying to set up a simple server.js file that listens on port 80 and serves /dist/index.html file when users enter root address, so example.com
This is my project structure
dist/
index.html
bundle.js
node-modules/
package.json
server.js
You can create a static server with express:
server.js
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/dist'));
app.listen(8080, function() {
console.log('Listening on port: ' + 80);
});
You simply run node server.js to get the static server. This app can also be deployed.
http-server is a simple method of serving from your file system. Install that, then just run http-server -p X in the command line in your project folder, substituting your port number for X

Categories

Resources