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
Related
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.
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.
I have an application using Node.js with Aurelia on the front-end, which I want to deploy on Heroku.
To run the app locally, I need to execute following commands:
1. npm start
2. cd public > gulp watch
After installing heroku-cli, tried publishing it using git push heroku master.
The problem is, Heroku only runs npm start when it's deploying the app on cloud. So it is able to start the server.
However, it doesn't know anything about cd public and gulp watch.
My question is, how can I tell Heroku to change directory to public and execute gulp watch command, once it has started the server?
Edit:
I forgot to address an important point. Since you only mentioned Aurelia in your question, I (wrongly) assumed that that's all you had.
Ultimately, for a production app you'll want to have a proper webserver hosting your Aurelia app.
Example:
For Aurelia apps I've built, I typically have 3 distinct processes running, each with their own port (or hostname):
IdentityServer
ASP.NET Web Api
OWIN FileServer
The third one is what hosts my Aurelia app as a static bundle.
There is no gulp or anything like that involved here. The server doesn't even have npm installed and sees it just like any other server-side application. And that's exactly how I deploy it; no node-related commands needed.
If you're using nodejs for your server-side stuff, use http-server to serve the static bundle.
When you host your aurelia app within your own serverside application you get the added benefit of being able to send some bootstrapping configuration directly along with the bundle, so you don't have to hard-code urls and such.
That's what I implied with "don't host a static site on heroku": bundle it up, and let your web application host it. My original answer would only apply if there is no server-side stuff involved.
Original answer:
It's generally not recommended to host static sites on heroku, see this blog post. The bottom line is that Aurelia sites are static, and a static site doesn't need an app server. It's unnecessarily expensive and doesn't have as good distribution as most CDNs do.
With that said, if you insist on hosting a static Aurelia site on Heroku then your best bet is to combine all your script calls into a single call which, as you say, already runs. So make your npm start script call gulp watch.
You'd probably want to npm install your dependencies and call ../node_modules/.bin/gulp watch instead of calling gulp globally.
When it comes to Heroku however, gulp watch in itself probably won't work because that will start a development server which will have no port binding in Heroku. It will run, but it won't be accessible from the outside.
gulp watch is not something you want to run on a server anyway because it will watch for file changes (which never happen there) and run things like browsersync which will be useless. Just bundle your app and start a normal http-server or better yet, upload the bundle ready-to-start into the correct folder and you're done.
You want to build your app and then deploy as if it were a fully-compiled, static application. With Aurelia CLI, that would be au build --env prod and then copy the scripts folder, index.html, and any dependencies like css, fonts, etc. to a separate folder. gulp build works the same way.
From there, you will publish the compiled app to Heroku as shown in this medium.com article:
https://medium.com/#winnieliang/how-to-run-a-simple-html-css-javascript-application-on-heroku-4e664c541b0b
The main part of the article is below, but here is the kicker - you are "tricking" heroku into thinking it is a PHP app. Serious!
Head to root directory of the repo that contains index.html which dictates the main HTML page.
Run touch composer.json to create a file called composer.json.
Add the following line: {} inside.
Run touch index.php to create a file called index.php.
Add the line: <?php include_once("index.html"); ?> inside.
Now update the repo on Github if it’s connected to your account or Heroku command git push heroku master . Wait for the automatic deploy to work its magic and tada!
There are some other steps to make your compiled app into a repo (ie, git init) but this should work for you. It did for me.
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
I'm developing an app, using MEAN.js and its generator (https://github.com/DaftMonk/generator-angular-fullstack), and Openshift as a hosting.
The project template of the generator includes a script (server/config/seed.js) to populate the database with two users.
In localhost, it is called automatically, but I also can call it using node server/config/seed.js (suppose you're on the root app directory).
The problem is, when I deploy it to Openshift, I run it and no error is reported, but the mongodb database is not updated. The exactly steps I do to run it on Openshift are:
Connect to ssh: ssh ....
cd app-root/runtime/repo/
`node server/config/seed.js``
What am I missing?
Thanks in advance.
You have few options:
in server/config/production.js add
seedDB: true
or change NODE_ENV to development coz during 1st deployment it is set to production
then
grunt
grunt:buildcontrol:openshift
should be working now