What's the equivalence of "Node app.js" on a file domain? - node.js

On localhost all there is to do is type "Node app.js" to run node locally, but I am trying to do this on a domain that I do not own but has Node installed. "https://www.'example'.com/MyID/myapp" is the example I will be using, do note that I only have access to edit everything in "MyID" file and not "https://www.'example'.com". the problem is that I do not know how to run node on the domain. The IDE I'm using is codetasty and it does not say how to run node on their sandboxes either.

This is a tough question because there are a lot of misunderstandings here. Node is a process that runs a JavaScript file. You can access http://localhost like a website because your node app creates a server that listens to a port and responds to the HTTP requests made by your browser. It seems like you are asking how to run node on a file hosted online. That may be possible but I think you actually want to run the node server on your web host and then connect to that instance with your browser. Most webhosts don't allow you to run node so you may need something like digital ocean to do so.

Related

React JS Front-end & Node js Server deployment on shared hosting

I am working on react js project & using node js for backend(to handle DB as well as to run CRON JOBs). At my local machine I had created 2 folders "botclient" & 'botserver". I starts the server using "node server.js" in 1 command prompt & in another command prompt, I satrs client using "npm start". Now in browser I use "http://localhost:3000 & my application works.
Now my client has given me 1 shared hosted domain something like http://mybot.hismaindomain.in.
Now he has given me cPanel credentials to deploy this app. I have no idea how should I deploy it on server. I had tried lots of threads like "running nodejs react in one directory" or "react js nodejs deployment on hosted server" But I could not deploy the application on Hosted server.
I am total newbie in this technology so please help me to solve this problem. Whether I am doing wrong folder structure? Whether I need 2 sub domains?(for client and server separately)
Please help me. Thanks in advance.
(This should be a comment but I csan't comment yet)
Maybe try using cPanel cron job to run the node command
You do not need two sub-domains. Neither do you need a specific folder structure to have shared hosting.
To run node two or more servers on the same host, from the same repository, from just one command line, you can use a package like 'concurrently' or 'npm-run-all', which lets one npm start-command activate a number of processes in parallell. For example: concurrently \"node server.js --port 4000\" \"webpack --config client/webpack.config.js --port 3000\" \"node cron-jobs.js\". In this example, the server is in the project root, and the react app is in client subfolder with its own package.json.
Then, to share a domain it is necessary to setup a reverse proxy, so that the backend's port can be accessed via a designated path, ie: /api. There are many ways to do it and there are others who would explain it better than me. Try google react node config reverse proxy or similar. Good to know is that the easiest solutions might be limited to a dev server, while the more complicated ones (like nginx) are independent on your tech stack (which you might not need).
For cron jobs, you could try a package like 'node-cron', check this guide for the basics

Deploying MEAN stack application to Heroku

I am trying to deploy an application I am building with the MEAN stack to Heroku and am having some trouble. I think the issue is with the structure of my application. I have all my server code in a folder called server, which has its own package.json and src folder that contains the actual server code.
Right now I am simply trying to get Heroku to deploy the client side of the application. I am only getting an error... I know that the database and server are not running but I cannot even get past the initial displaying of the app. I have one web dyno set up to run ng serve (npm start) on the app.
If someone would be willing to look at the structure of my application and sees why I am unable to deploy to Heroku without really digging into the code, that would be much appreciated.
Here is the code
Please note that it is on the deploy branch, and this is on purpose. I do not want to push anything to the master until I am sure it is working.
The Angular web server targets localhost:4200 by default. That can be changed with a couple command-line options. --port accepts a port and --host accepts a host IP address.
So you could modify the ng serve script as follows: ng serve --host [host-ip] --port [port-number] --disableHostCheck. That last flag (--disableHostCheck) tells the dev server to bypass host checking when normally ng serve fails on anything except localhost. Terrible idea if meant for anything except private development/testing.
Another issue: Heroku runs off web dynos and from what I understand about them, they use a random port and IP each time. While the random port is stored under the variable $PORT, the IP does not seem to have a similar variable. Web dynos keep that information to themselves.
Heroku does offer this command: heroku local web. It runs your application on localhost:5000. That means ng serve --port 5000 works perfectly with this command. This should tell you how your front-end will run on Heroku. Your angular dev server will function as expected too.
For actual deployment to Heroku, use that express server of yours. Run ng build from your file system and it will spit out an index.html in the dist folder. This file holds your entire front-end application. You can then upload that file into your browser from the server.
For express that usually breaks down into:
app.get('*', function(req, res) {
res.sendFile('path/to/index.html');
});
Hopefully this helps! Let me know if I missed the mark anywhere.

Do I need to manually restart my app on EC2 if the server restarts?

This is my first time with EC2 so keep that in mind. I spun an EC2 instance and put a really basic nodejs/express app up on it. I connected to the ec2 server via the terminal on my personal computer and ran node app.js to start the app and everything is running fine. The part I am confused about is how long this will run for. Ideally, I just want it to sit there and not touch it and have it run for hopefully years. Will it do this? If not what do I need to do? What if the server restarts for some reason? What is the common practice here?
Go to root directory of your project and type this command to run the server permanently.
sudo npm install forever -g
forever start -c "node app.js" ./
This blog may be helpful, in setting up node for production environments

Programmatically watch and restart node server

I was getting tired of using FileZilla every time I wanted to push a change to my server hosting my website and so I set up a github repo and linked it to my server so that changes and pushed right to the server.
However, my backend is written in node and so each time I update my server.js file I have to restart the server. With "node server.js"
Is there a way to watch the file and programmatically restart the node server when an update is detected?
If it helps, my serve is Ubuntu Linux running apache2
You could try writing something yourself, or use one of the popular libraries that are already out there:
https://github.com/petruisfan/node-supervisor supervisor server.js
https://github.com/remy/nodemon nodemon server.js

Getting 502 error in browser for a NodeJS app run with Forever

I have a SailsJS app set up on a Webfaction server. Everything works nicely (site can be accessed through browser, console works) when I run the app via any of the following commands, with and without the --prod param:
sails console,
sails lift,
node app.js
However, when I try to run the app with forever using forever app.js I get a 502 error, as if nodejs server isn't even running. When I run forever list I can see app.js listed among running processes.
How can I have my app run with forever?
Forever is considered outdated by many in the Node community, and thankfully, has been replaced by several other fantastic (dare I say, better) tools.
If you're running a newer flavor of Ubuntu, you can always install systemd and kick off the application that way. If you're seeking something more streamlined, Phusion Passenger might be your ticket. It has a long track record of successes, and I wouldn't hesitate to toss it into production.
I managed to solve this issue; the problem occurred due to SailsJS migration prompt which shows up when you start the server. Running app.js with forever worked, but the server didn't start because the script hanged waiting for a prompt reply. If you encounter this issue just make sure you have your migrate option set in model config to avoid running into migration prompt.

Resources