How to deploy React App client and Node Server to cPanel - node.js

I am trying to deploy a react app and a node server to my domain http://cv-devlabs.com/ with cPanel but failing to run. Most of the time I'm getting a "server responded with the 404 error".
I have tried methods from "hosting react app and express server cpanel" and "How to deploy a react app on cPanel?" and failed at both. I'm assuming I did something wrong here.
My file structure is:
Root-Folder--
-client
--build
--node_modules
--public
--package.json(client)(content added below)
-.gitignore
-package.json(server)(content added below)
-procfile
-server.js
Package.json (client)
"name": "client",
"homepage": "http://cv-devlabs.com/vidci-vid2/",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:8000",
Package.json(server)
{
"name": "vidci-vid",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"dotenv": "^8.2.0",
"express": "^4.17.1",
"socket.io": "^2.3.0"
},
"scripts": {
"start": "PROD=true node server.js",
"postinstall": "cd ./client && yarn"
}
}
This is working fine on Heroku but on cPanel, it's not. The whole project can be found at https://github.com/ConsultVerraton/vidci-vid.git
Thanks to anyone who can help and thanks to anyone who can try. Do let me know if more information is needed.
Thanks

You cannot host a node.js app in most providers working with cpanel. If you want to host a node.js app you should deploy it on a vps. You can follow this tutorial for example:
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04
But in these cpanel providers, you could host a static react app (for example an app build with create-react-app). This kind of app does not need node.js at runtime. So you can build the assets and push them to your cpanel host.
You can follow this tutorial:
https://dev.to/crishanks/deploy-host-your-react-app-with-cpanel-in-under-5-minutes-4mf6

Yes you can, but it depend if the host provider allow it.
I'm actually running a express server on a cPanel account.
I didn't found the official documentation of cPanel but this is a pretty good one: How to create a Node.js application with cPanel using the Node.js Selector
If your looking to do it in command line, this the official documentation from cPanel :How to Install a Node.js Application.

I don't know about react but for anyone who wants to know how to deploy node js app to Cpanel this is a good source for him, this explains thoroughly how to deploy node js app to cpanel please check this

Related

Heroku Node.js Discord Bot not starting

I am currently facing an issue where my Discord Node.js bot is not starting on Heroku for some reason, despite doing everything that I could possibly find on this forum, the official documentation, youtube videos, anything.
If I run the index.js file on my computer, the bot successfully logs in, and commands I have added so far work perfectly fine. But of course I want to offload this payload to a server, that is Heroku.
My project is on a private repository on GitHub, with which I have already set up auto deploy on the Heroku dashboard (main branch).
My folder structure:
node_modules/
src/
Commands/
Data/
Events/
Structures/
index.js
.gitignore
package-lock.json
package.json
Procfile
README.md
The content of my Procfile is this (without quotes): "worker: node src/index.js"
In package.json, relevant parts are:
"main": "src/index.js",
"scripts": {
"start": "node src/index.js"
},
"engines": {
"node": "16.x",
"npm": "8.x"
},
"repository": {
"type": "git",
"url": "git+https://github.com/******/******.git"
},
"private": true
Environmental variables are also set up on the Heroku Dashboard, and it is on the gitignore list, and found at src/Data/config.json file.
I have checked the Heroku logs, and it's literally empty. It didn't even try to start yet, and on the Resources tab of the dashboard, it says "This app has no process types yet. Add a Procfile to to your app in order to define it's process types."
However, I totally have a Procfile in the folder, and on GitHub as well.
I also added the Node.js Buildpack to my app on the Heroku Dashboard.
Guys, what am I missing here? How could I finally get Heroku to launch my Node.js?
I'm gonna close this help request by a quote from one of my favorite games (We Were Here Together): "Please somebody help!" :)
Okay, figured it out. For someone in the future that runs into the same issue:
Solution was to disconnect, then reconnect the GitHub repository on the Heroku Dashboard.
Users must have a Procfile described in the question, where the js file points to the index file, with directory relative reference.
package.json must contain the start script. Version number declaration is not mandatory.

Error using Postgres with a newly created React app: Module not found: Can't resolve 'dns'

I am probably missing something very obvious here, but I keep getting the following error when trying to use Postgres from a newly created React app
Module not found: Can't resolve 'dns' in '/Users/tarek/test/node_modules/pg/lib'
Here are the steps to reproduce:
Create a fresh React app using create-react-app
Add the Postgres
module using npm install pg --save
Add the following line to
App.js: const { Client } = require('pg');
Type npm run start and you'll get the above error.
What am I missing here?
**Node version: v8.12.0
** package.json
{
"name": "test",
"version": "0.1.0",
"private": true,
"dependencies": {
"pg": "^7.4.3",
"react": "^16.5.1",
"react-dom": "^16.5.1",
"react-scripts": "1.1.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
The issue is that React apps are meant to run in the browser, and browsers can't access databases directly. Even if you did find a way to do this, it would only work locally and not in production. Consider that if the database is on your machine and your app available on the internet, there would be no way for a remote user to get access to the data on your machine in the way you are describing.
In order to use a database with React, you'll need to set up a server—possibly written in Node (with Express)—that will serve your application and data for it.
The server receives requests from different users and responds with the appropriate resources. For example if they go to your website (e.g. example.com), your server should respond with index.html for your project (in this case it would be the index.html for your React app). Then to get data from your database, you can set up an api with routes like example.com/api/model/1 so that your react app can request the instance of model with id 1 from your server, which will grab it from the database and send it over to the React app.
Look into the MERN (Mongo Express React Node) stack for more detail on how all these work together. Even with Postgres the process will be pretty much the same as with Mongo.

How do you update Openshift NodeJS Cartridge's Express from 3.x to the latest 4.x?

When Openshift creates a Node.js cartridge it includes a version of Express 3. My app is an Express 4 app and fails to start under the default Openshift setup. Even if my app's package.json has the line "express": ">=4.9.0" in dependencies.
4.9.0 happens to be the version that is embedded in my app's project but is ignored by Openshift when started there. So apparently I need to update Openshift's version to 4. I can confirm that the app works as designed and intended on my local computer.
How do I update Openshift's Express, which is outside the app, from version 3 to 4 ?
I made sure that my /package.json includes something like this under dependencies:
"express": "~4.11.1"
Personally, I retired my /bin/www content by removing that out of /package.json:
...
"scripts": {
"start": "node server.js"
},
"main": "server.js"
}
...and migrated much of /bin/www back into /server.js.
That seemed to be the only way I could get Express4 to work on OpenShift.
/server.js needs a shebang a the top "#!/bin/env node"
/package.json gets the mod above
I've got my Express4 app running now with MongoDB support. Seems to be happy. Pushes/builds/logs success.
Which works for me is:
"main": "./bin/www",
"scripts": {
"start": "node ./bin/www"
}

NodeJS - Deploy socket server in Jelastic

I have a socket server that I want to deploy in my Jelastic NodeJS setup. It is made of 2 files, being server.js the main file, the one started/opened by node, and worker.js the file opened as a child process by the server when required (each child handles a socket).
I never deployed a project via Jelastic or any other cloud PaaS/hosting. How should I proceed to do this?
I know that I have to write the package.json, but that's all I know.
So far, this is what I've got:
//package.json
{
"name": "Myserver",
"version": "1.0.0",
"description": "My Server",
"main": "server.js",
"dependencies": {
"microtime": ">=1.2.0",
"microseconds": ">=0.0.2",
"mysql": ">=2.5.4",
"point-in-polygon": ">=0.0.0"
},
"devDependencies": {},
"scripts": {
"start": "node server.js"
},
"author": "Magister"
}
I searched on their docs but all they have is how to deploy a WEB application, but my server is not web-related, so that information is not relevant.
Any suggestions?
Do you have a public IP? Most likely WebSocket app won't work behind the proxy so far

HTML5 application on Heroku

I wish tho host my HTML5 application on Heroku. But I must choose one of the platforms (Java, Python, Node.js, etc) for my app. How can I run my application under Node.js? I have been able to create and deploy a simple Node.js app in Heroku however I am yet to figure out how can I merge my HTML5 app into this Node.js app.
I hope I make sense.
Tanks
"HTML5 app" is very non-descriptive and generic. If you mean an entirely client-side HTML application with no real backend logic, than that's trivial with node.js on heroku. If you put this in the root of your git repo in a file called server.js:
var app = require('express')();
app.use(express.static('app'));
app.listen(process.env.PORT);
Then put all of your HTML5 app in a directory called 'app' and node will serve it up.
Also in the root of your repo you'll need a Heroku Procfile with the following:
web: node server.js
Finally, you'll also want a package.json in the root of your repo so that Heroku can install your dependencies:
{
"name": "my-app",
"description": "My application",
"version": "0.0.1",
"dependencies": {
"express": "3.4.0"
},
"engines": {
"node": ">=0.10.0"
}
}
you can use ruby as well. rack based app Heroku rack based app
You need to create a config.ru file and put your HTML content into public folder
Here are more details about creating a static site based on rack app. Creating Static Sites in Ruby with Rack
Hope this helps!

Resources