Namecheap: Node JS Express App - App Route return 404 not found - node.js

Trying to get Simple Express Application up using NameCheap Shared Hosting.
I have set up my Node JS application as Described here NodeJS NameCheap Docs
Current Setup:
Application Root: url.com
Application URL: url.com
Application Startup File: server.js
I have ran NPM Install using the button provided
I have tried loading the URL http://url.com/hello Expecting Hello World to displayed in the Page.
var express = require("express");
var app = express();
const port = 3001;
app.set("port", port);
app.get("/hello", function(req, res) {
res.send("hello world");
});
app.listen(app.get("port"), () =>
console.log("Started listening on %s", app.get("port"))
);
The results I am getting when navigating to http://url.com/hello:
Not Found
The requested URL /index.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Namecheap only tells you how to configure the nodejs app however their hosting is based on cPanel which requires you configure the webserver (apache generally). Once you get an application running there’s a special button to register it for the apache configuration aka let it run from your domain. I don’t know the steps by heart but you should ask NC support to direct you to their documentation for configuring apache to run a nodejs app you configured.
If they do not link an article from their knowledge base use this link: https://confluence1.cpanel.net/plugins/servlet/mobile?contentId=17190639#content/view/17190639
Basically what you need now is to configure cPanel or ssh into your server and test your app locally. There’s a number of things that could cause your issues like incorrect apache configuration (your default port 80 is looking for php app), port not open/firewalled, application not registered - and all of this is cPanel specific.
To make sure you are reading the correct document check in namecheap cpanel for the docs button and review all the above. It should be obvious what needs configured - your nodejs code is probably not the cause here

In my case, it was the problem with .htaccess file. Adding the following rules in my .htaccess file present in the website's public directory helped me:
# CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/<user>/<your_nodejs_app_folder>"
PassengerBaseURI "/."
PassengerNodejs "/home/<user>/nodevenv/<nodejs_app>/<version>/bin/node"
PassengerAppType node
PassengerStartupFile <startup_script>.js
# CLOUDLINUX PASSENGER CONFIGURATION END
Make the required changes in the above rules before pasting them in your .htaccess file. Also, just in case, make sure the port you are using is open, via customer support.

Related

Can't access Node.js server on web server

I'm trying to test a simple Node.js server on my webserver. The problem is that I can't access the Node.js server from my chrome browser and I have searched without any success.
This is my basic server.js script (server side)
var app = require('express')();
app.get('/test', function (req, res) {
console.log('web page opened');
});
app.listen(3000, function () {
console.log('Listening on port 3000');
});
When I run the node in console I get result as expected
public_html$ node server.js
Listening on port 3000
Now when I try to access the URL like this:
http://xxx.xxx.xxx.xxx:3000/test the connection times out and I do not get anything.
This prevents me from using $.ajax form to send data to my node server and the jquery request fails with error as connection timed out issue as well because the URL:3000 with my nodejs port is not accessible.
It seems like my host (Cloudways) does not allow access on any port. If that is case, what can I do really in this situation?
Note: I do not have root access to the server, they can't give root access for security.
Actually going thru Apache to access your app server, nodeJs in your case, is the standard way to avoid security vulnerabilities etc.. You can configure your Apache server as follows:
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName www.example.com
ServerAlias example.com
ProxyPass / http://localhost:3000/test/
ProxyPassReverse / http://localhost:3000/test/
</VirtualHost>
The you would call your app at http://xxx.xxx.xxx.xxx:80/test or simply at http://xxx.xxx.xxx.xxx/test since 80 is implied for HTTP and apache will call http://xxx.xxx.xxx.xxx:3000/test for you.
EDITED: This should work thru .htacccess too - which is what, it seems cloudways wants you to do : https://support.cloudways.com/what-can-i-do-with-an-htaccess-file/
Best way to test node.js is to use node process manager on your server like https://www.npmjs.com/package/pm2.
This will save a lot of time for deployment. but you will need root access to install this package. if you does not have root access then ask your hosting provider to DO install these packages for you.
If in any case you did not get root access and you have no access to open port and install packages for your project. Then use any other hosting like https://www.linode.com/.

Run node.js on cpanel hosting server

It is a simple node.js code.
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type' : 'text/plain'});
res.end('Hello World!');
}).listen(8080);
I uploaded it on cpanel hosting server and installed node.js and run it.
If a server is normal server I can check script result by accessing 'http://{serverip}:8080'. But on cpanel is hosting domain and sub domain and every domain is matched by every sites. Even http://{serverip} is not valid url.
How can I access my node.js result?
Kindly teach me.
Thanks.
bingbing.
Install/Setup NodeJS with CPanel
1. Log in to your account using SSH (if it is not enabled for your account, contact the support team).
2. Download Node.js
wget https://nodejs.org/dist/latest/node-v10.0.0-linux-arm64.tar.xz
3. Extract the Node.js files
tar xvf node-v10.0.0-linux-arm64.tar.xz
4.Now rename the folder to "nodejs". To do this, type the following command
mv node-v10.0.0-linux nodejs
5. Now to install the node and npm binaries, type the following commands:
mkdir ~/bin <br> cp nodejs/bin/node ~/bin
cd ~/bin
ln -s
../nodejs/lib/node_modules/npm/bin/npm-cli.js npm
6. Node.js and npm are installed on your account. To verify this, type the following commands
node --version
npm --version
The ~/bin directory is in your path by default, which means you can run node and npm from any directory in your account.
7. Start Node.js Application
nohup node my_app.js &
8. Stop the Application
pkill node
9. Integrating a Node.js application with the web server(optional)
Depending on the type of Node.js application you are running, you may want to be able to access it using a web browser. To do this, you need to select an unused port for the Node.js application to listen on, and then define server rewrite rules that redirect visitors to the application.
In a text editor, add the following lines to the .htaccess file in the/home/username/public_html directory, where username represents your account username:
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:XXXXX/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:XXXXX/$1 [P,L]
In both RewriteRule lines, replace XXXXX with the port on which your Node.js application listens.
To run a Node.js application on a managed server, you must select an unused port, and the port number must be between 49152 and 65535(inclusive).
Save the changes to the .htaccess file, and then exit the text editor. Visitors to your website are redirected to the Node.js application listening on the specified port.
If your application fails to start, the port you chose may already be in use. Check the application log for error codes like EADDRINUSE that indicate the port is in use. If it is, select a different port number, update your application’s configuration and the .htaccess file, and then try again.
cPanel typically runs Apache or another web server that is shared among all the cPanel/unix accounts. The web server listens on port 80. Depending on the domain name in the requested URL, the web server uses "Virtual Hosting" to figure out which cPanel/unix account should process the request, i.e. in which home directory to find the files to serve and scripts to run. If the URL only contains an IP address, cPanel has to default to one of cPanel accounts.
Ordinarily, without root access, a job run by a cPanel account cannot listen on port 80. Indeed, the available ports might be quite restrictive. If 8080 doesn't work, you might try 60000. To access a running node.js server, you'll need to have the port number it's listening on. Since that is the only job listening on that port on that server, you should be able to point your browser to the domain name of any of the cPanel accounts or even the IP address of the server, adding the port number to the URL. But, it's typical to use the domain name for the cPanel account running the node.js job, e.g. http://cPanelDomainName.com:60000/ .
Of course port 80 is the default for web services, and relatively few users are familiar with optional port numbers in URLs. To make things easier for users, you can use Apache to "reverse proxy" requests on port 80 to the port that the node.js process is listening on. This can be done using Apache's RewriteRule directive in a configuration or .htaccess file. This reverse proxying of requests arguably has other benefits as well, e.g. Apache may be a more secure, reliable and manageable front-end for facing the public Internet.
Unfortunately, this setup for node.js is not endorsed by all web hosting companies. One hosting company that supports it, even on its inexpensive shared hosting offerings, is A2Hosting.com. They also have a clearly written description of the setup process in their Knowledge Base.
Finally, it's worth noting that the developers of cPanel are working on built-in node.js support. "If all of the stars align we might see this land as soon as version 68," i.e. perhaps early 2018.
References
Apache Virtual Hosting -
http://httpd.apache.org/docs/2.4/vhosts/
Apache RewriteRule Directive - http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
A2Hosting.com Knowledge Base Article on Configuring Node.js - https://www.a2hosting.com/kb/installable-applications/manual-installations/installing-node-js-on-managed-hosting-accounts
cPanel Feature Request Thread for node.js Support - https://features.cpanel.net/topic/nodejs-hosting
Related StackOverflow Questions
How to host a Node.Js application in shared hosting
Why node.js can't run on shared hosting?
Yes it's possible, but it has few dependencies which may or may not be supported by either your cpanel hosting provider or the plan you opt in for.
Below steps that I'm mentioning is just for a demo purpose. If you are a student or just want to play with it you can try it out. I'm not a security expert so from security point of view how good it is I really don't know.
So with that being said let's see how I configured it. I have hostinger cpanel hosting subscription and following are the steps:
Enable SSH ACCESS
Connect to shared machine via ssh
Check your linux distro and download & setup node js
In my case following are the commands for that:
Downloading node & extracting it using curl
curl https://nodejs.org/dist/v12.18.3/node-v12.18.3-linux-x64.tar.gz |tar xz
This will download & extract node & create a directory. You can confirm that using ls command as shown in the image below.
At this point you can check the versions as shown below
as you can see for the node command it's okay but for the npm command we have modify it as follows
./node-v12.18.3-linux-x64/bin/node ./node-v12.18.3-linux-x64/lib/node_modules/npm/bin/npm-cli.js --version
Further we can create alias to make life little easier
check the below images for that:
I tried using bashrc/bash_profile but somehow it didn't work .
And that's all node server running on a shared cpanel machine.
Now I wanted to have an express js based rest api support in this case. The problem with that is it will be locally hosted on the port I'll give. Check the below example:
var express=require('express')
var app=express()
app.get('/', function (req, res) {
res.send('hosting node js base express api using php & shared hosting a great way to start yjtools')
})
console.log("listening yjtools node server on port 49876...")
app.listen(49876)
The problem here is even though it will execute I'll not be able to access it over the network. This is because we only get fixed predefined ports (like 80,21,3306 etc.) which are allowed/open on the shared cpanel machine. Due to this the express app I hosted will only available locally on 49876 port.
Let's see what do we have:
An express js based app hosted locally on cpanel machine.
Php based hosted Apache server available over http/https.
So we can make use of php with redirect rule set and curl to bridge the gap.
Following are the changes I did to make it work:
In .htaccess file add a redirect rule, say domain/api is what I want my rest api path to be.
RewriteRule api/(.*)$ api/api.php?request=$1 [QSA,NC,L]
In the api/api.php file (this is the path I choose you can choose any path)
<?php
echo "Hello ".$_REQUEST['username'];
echo '<hr>';
$curl = curl_init('http://127.0.0.1:49976/');
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//Get the full response
$resp = curl_exec($curl);
if($resp === false) {
//If couldn't connect, try increasing usleep
echo 'Error: ' . curl_error($curl);
} else {
//Split response headers and body
list($head, $body) = explode("\r\n\r\n", $resp, 2);
$headarr = explode("\n", $head);
//Print headers
foreach($headarr as $headval) {
header($headval);
}
//Print body
echo $body;
}
//Close connection
curl_close($curl);
?>
And on the ssh prompt just run the app.js file
node api/app.js
Below are the images for this working in action:
Here is the similar thing which I referred for my program, so we can also make this node call via php itself.
Now I have express based rest api support , angular app hosted and mysql for database everything on cpanel.
You can use any domain pointed to that cPanel server and instead of accessing http://server-ip:8080 try accessing http://domain.tld:8080. By default cPanel does not bind on port 8080. Be sure to check if there is any firewall on the server. If it is, then allow incoming connections on tcp port 8080. Depending on your WHM server configuration, it should also work with http://server-ip:8080
cPanel Version 80 has nodejs 10.x support: https://documentation.cpanel.net/display/80Docs/80+Release+Notes#id-80ReleaseNotes-InstallanduseNode.jsapplications
Install and use Node.js applications
You can now install and use Node.js applications on your server. To
use Node.js, install the ea-nodejs10 module in the Additional Packages
section of WHM's EasyApache 4 interface (WHM >> Home >> Software >>
EasyApache 4).
You can register Node.js applications in cPanel's Application Manager
interface (cPanel >> Home >> Software >> Application Manager). For
more information, read our Guide to Node.js Installations
documentation.
For Application Manager to be enabled: https://documentation.cpanel.net/display/80Docs/Application+Manager
Your hosting provider must enable the Application Manager feature in
WHM's Feature Manager interface (WHM >> Home >> Packages >> Feature
Manager).
Your hosting provider must install the following Apache modules:
The ea-ruby24-mod_passengermodule. Note: This module disables Apache's
mod_userdir module.
The ea-apache24-mod_env module. Note: This module allows you to add
environment variables when you register your application. For more
information about environment variables, read the Environment
Variables section below.
The ea-nodejs10 module if you want to register a Node.js™ application.
You can see how application manager looks like in this Youtube video:
https://www.youtube.com/watch?v=ATxMYzLbRco
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

socket.io cannot communicate with back-end through express

I am using socket.io for fetching some data from the express app. Everything works fine on localhost:8080 but when i deploy my code to the server, client side socket.io cannot communicate with back-end socket.io through express server. I've an apache on the server that forwards everything to localhost:8080 including domain.com/socket.io/?transform=polling... and it extends the request to the express server but express returns a 404 (it comes from the express, not from apache). I am out of ideas, what could be the problem? Here is my code:
express = require "express"
jade = require "jade"
fs = require "fs"
class Bootstrap
_self = undefined
routes:
DEFAULT_PATH: "/"
TEMPLATE_PATH: "/load/:view"
DIRECTIVE_PATH: "/directive/:template"
options:
templatePath: "#{__dirname}/../src/templates"
isDev: "#{__dirname}/../dev"
contentPath: "#{__dirname}/../frontend"
libraryPath: "#{__dirname}/../bower_components"
port: 8080
status:
notFound: 404
isDev: undefined
constructor: ->
_self = #
#isDev = fs.existsSync #options.isDev
#app = express()
#app.use "/frontend", express.static(#options.contentPath)
#app.use "/bower_components", express.static(#options.libraryPath)
#app.set "views", #options.templatePath
#app.set "view engine", "jade"
#app.engine "jade", jade.__express
#app.get #routes.DEFAULT_PATH, (request, response)->
appData =
data:
isDev: _self.isDev
response.render "index", appData
#app.get #routes.TEMPLATE_PATH, (request, response)->
view = request.param "view"
response.render view
#app.get #routes.DIRECTIVE_PATH, (request, response)->
template = request.param("template").replace ".html", ""
response.render "directives/"+template
#app.use (request, response, next)->
_self.logger.warning "404 Not Found!: " + request.originalUrl
response.status(_self.options.status.notFound)
appData =
data:
isDev : _self.isDev
request: request
response.render "404", appData
#server = #app.listen #options.port
#io = require("socket.io").listen #server
#logger = require("./logger.js")
#logger.init #isDev
#socketConnector = require("./live.js")
#socketConnector.init #io
#
new Bootstrap()
You can find the entire code here: https://github.com/eyurdakul/ejder.be
Since you specifically said it works locally and I don't see anything special in your code, I really think the issue resides in your Apache config.
There is a module called mod_proxy_wstunnel you need to have and enable in order to get your WebSocket traffic to work and get proxied correctly to your express application. Either you have Apache 2.4 or, you'll need to apply this patch found in this blogpost and compile again. There is also another blog detailing step by step what you should do under Ubuntu.
After making sure you have the module and its loaded, you'll have to tweak your config to add some WebSocket proxying using the ProxyPass directive
ProxyRequests Off
ProxyPass "/socket-io/" "ws://localhost:8080/socket.io/"
ProxyPassReverse "/socket-io/" "ws://localhost:8080/socket.io/"
You might also want to try to use a rewrite condition on the Upgrade header that is sent with WebSockets:
RewriteEngine On
RewriteCond %{HTTP:Connection} Upgrade [NC]
RewriteRule /(.*) ws://localhost:8000/$1 [P,L]
ProxyPass / http://localhost:8000/
has seen in this comment or this specific thread in general, which exposes a few other possible solutions you can try.
A debugging step that you should take is to have a mock client run in your express app and try to connect to the server port using localhost, or whatever the loopback IP address is. In other words (purely as a temporary testing tool) you place a socket.io Node client into your Express app that gets instantiated and immediately tries to connect to the Socket.io server (which is also in the express app). This should work.
If it does work, you've established that your express server is not the problem. Then, on the client you just added to your express app, you should change the connection address from the local IP to the actual IP address of the server.
If it doesn't work, your problem is probably server side.
My guess is that your apache server is forwarding the polling requests properly, but when Socket.io tries to make the handoff to websockets, Apache is denying the WS request and socket.io is handling this denial internally -- which is why the 404 seems to come from express.
Even I had same issue. The port on which the server is listening , is that open on server. As on local all the ports are open but on server , it's not the case.

502 Bad Gateway with nginx | Google App Engine | Node JS

I am hosting the web app on Google Cloud Platform with App Engine and I am using ExpressJS and MongoDB, which is hosted on mLab.
Everything worked well until 1/1/2017. I had vm:true before and now was forced to change the env to flex. Now I am getting 502 bad gateway error with nginx. App engine doesn't allow us to change the nginx config file.
I had tried the suggestion from this post: Google App Engine 502 (Bad Gateway) with NodeJS but still doesn't work.
For some reason, I have another app with exactly the same setting on app engine and it works perfectly.
Any suggestion will be greatly appreciated. Thank you.
app should always listen to port 8080, google forwards all request from 80 to 8080
https://cloud.google.com/appengine/docs/flexible/custom-runtimes/build#listen_to_port_8080
check out the logs for any deployment errors
$ gcloud app logs read
I have came across a similar issue with the code provided by this tutorial (https://cloud.google.com/nodejs/getting-started/authenticate-users)
And found there was a missing dependency. I fixed the missing dependency and the app is deployed and working fine.
Details into the issue: https://github.com/GoogleCloudPlatform/nodejs-getting-started/issues/106
I had the same problem with Express. What solved it for me was to not provide an IP address for the app.
So my old code would be:
var ip = "127.0.0.1";
var port = "8080";
var server = http.createServer(app);
server.listen(port, ip);
This would result in a 502 in app engine.
Removing the ip was the solution for me.
server.listen(port);
Set the host to 0.0.0.0
Port 8080 is set by default by the engine. In fact, you are not able to define the environment var PORT as it is reserved.
Run the next command (as mentioned by #sravan )
gcloud app logs read tail
and make sure it looks like this,
[Sun May 27 2018 10:32:44 GMT+0000 (UTC)] serving app on 0.0.0.0:8080
Cheers
Google App Engine uses an nginx front to load balance all requests for node.js apps. With nginx acting as a forward proxy, this error usually happens when the request the user is making in the browser is reaching nginx (you see the unstyled 502 bad gateway error page) but the nginx server is not able to correctly forward the request to your node app. There could be many issues why this is happening but here are some common ones:
By default, App Engine assumes your node app is running on 8080. nginx itself will run on 80 and forward the request to 8080. Check if your app's port number is 8080.
You app may have a hostname defined like a domain something.appspot.com or an IP 127.18.21.21 or the like. Remove any hostnames from your server.listen or config.json or vhost wherever. App Engine will take care of domains, IPs etc so you dont have to.
Your app may be crashing before its sending a response to nginx. Check the logs of both nginx AND your node app.
To check logs / find out what is going on use this guide https://cloud.google.com/appengine/docs/flexible/nodejs/debugging-an-instance#connecting_to_the_instance to SSH directly inside the VM behind app engine. There will be one docker process with nginx where you can see the nginx error log and one docker image with your node app to check your node app's error message.
I'm just wondering, based on the activity in this question and the timestamps, why hasn't Google updated its documentation to cover this issue!!! ???
Please take care of http also, while deploying, it should be http server not https
var server;
if (process.env.NODE_ENV == "dev") {
server = https.createServer(httpsOptions, app);
} else {
server = http.createServer(app);
}
A 502 is not necessarily an error with nginx itself, it can most often happen when the nginx proxy cannot talk to your app container (usually because your app failed to start). If you get a 502 after migrating to 'env: flex' this is most likely due to some code changes needed in your app as mentioned in Upgrading to the Latest App Engine Flexible Environment Release.
Checking your application logs for errors from NPM will also help to diagnose the exact reason for the failed startup.
Create a server and then check with a ternary condition if current environment is production or not, assign port '80' if current environment is development else assign process.env.NODE.ENV.
const app = require('express')();
const server = require('http').Server(app);
const port = process.env.NODE_ENV === 'production' ? process.env.PORT :'80';
server.listen(port, ()=> {
console.log('listening on port number *:' + server.address().port);
});
In my case, I had the same error due to google app engine update which trigged auto re-deployment of my React SPA to the google cloud vm. Then it leads to a build fail in the process because of incompatibility of runtime which is node 16.x.x. Compatible runtime was node 14.19.0. I had to specify node version in my package.json file and do the deployment again to fix 502 Bad Gateway error.
{
"engines": {
"node": "14.19.0"
}
}
Also refer:
https://cloud.google.com/appengine/docs/nodejs
https://cloud.google.com/appengine/docs/flexible/nodejs/runtime
Hope this helps with someone having this issue with React SPAs.

How can a node.js express application be deployed behind HAProxy?

I have a node.js express application running on machine1 on port 3000. I can successfully access my application via machine1:3000
I have HAProxy running at machine2. I updated haproxy.cfg as follows
frontend main *:80
acl url_mynodeapp path_beg -i /mynodeapp
use_backend mynodeapp if url_mynodeapp
backend mynodeapp
mode http
reqrep ^([^\ ]*\ /)mynodeapp[/]?(.*) \1\2
balance roundrobin
server machine1 1.1.1.1:3000
I can now hit my application at machine2/mynodeapp. However all relative links are now broken, including css and javascript (since they point to machine2 instead of machine2/mynodeapp).
How do you deploy an express application behind a proxy? How do you deploy an express application with a subfolder? I am coming from the java world were all web applications are name spaced inside the servlet container, thus giving each application its own subfolder.
Thanks,
Nathan
/mynodeapp isn't a subdomain. It's a subfolder. Subdomain would be mynodeapp.machine2
Subdomain hosting works fine with express, since that is not express's job, rather the front-end's.
As long as your front-end resolves consistently, (which it would with subdomains, not subfolders), express will receive expected urls from HAproxy
The only solution I could find was manually setting up the node application to run from a subfolder.
I used the solutions provided from How to handle relative paths in node.js / express?
and call functions from with ejs templates on node
var subfolder = '/mynodeapp';
app.set('view engine', 'ejs');
app.use(subfolder, app.router);
app.use(subfolder, express.static(__dirname + '/public'));
app.locals.createLink = function(uri) {
return subfolder + uri;
}
Then in ejs views, create links via function call createLink.
<script src="<%= createLink('/javascript/myjavascript.js') %>">
Home
Now I have a node.js express application running on at machine1:3000/mynodeapp.
I can then easily route thru a proxy and access my application at machine2/mynodeapp. Note: in the proxy config, just remove the regrep line.

Resources