This guide helps to resolve the “Cannot GET” URL error in a Node.js application. This issue often occurs when trying to access your Node.js app but the server cannot find a matching route.
In cPanel, Node.js applications are managed through Phusion Passenger, which handles routing based on the Application URL field set during app setup. Passenger uses this field to generate the root path of your application.
For example, if you enter “nodeapp” in the Application URL field, your app’s root path becomes /nodeapp rather than “/”.
Resolution:
1. Make sure your routes account for the Application URL specified in cPanel.
2. Using Express, the popular Node.js web framework, the example below shows how to handle this properly.
3. Before running the code, ensure that “nodeapp” (or your application path) is entered in the Application URL field when setting up the Node.js app in cPanel.
const express = require(‘express’);
const app = express();
app.get(‘/nodeapp/’, function(req, res){
res.send(“Hi from the root application URL”);
});
app.get(‘/nodeapp/demo/’, function(req, res){
res.send(“Hi from the ‘demo’ URL”);
});
app.listen(0, () => console.log(‘Application is running’));
In the example code, two routes are defined: /nodeapp and /nodeapp/demo. If your domain is yourdomain.com and you try to access http://yourdomain.com/nodeapp or http://yourdomain.com/nodeapp/demo, it works as expected. However, if you try to visit http://yourdomain.com/nodeapp1 or any incorrect path which is not defined in the code, you will experience the “Cannot GET” error.