Node.js application error message: "Cannot GET" URL
Learn how to resolve a problem that may occur when you try to run a Node.js application using cPanel's Node.js Selector.
This article discusses a problem that may occur when you try to run a Node.js application using cPanel's Node.js Selector.
Problem
You create a Node.js application using cPanel's Node.js Selector. When you try to view a page generated by the application, you receive the following error message:
Cannot GET /url/
In this error message, url represents the path you are trying to view.
Cause
The Node.js Selector uses Phusion Passenger to manage Node.js applications. When you create an application in the Node.js Selector, Passenger uses the value in the Application URL text box to create the root path. For example, if the Application URL text box is set to myapp, then the root path for the application is not "/" but "/myapp".
Note
This behavior is different from most other web environments, where "/" is typically the root path.
Resolution
To resolve this problem, you must include the application URL in your routes. The following code sample demonstrates how to do this using the popular Express web application framework. It assumes that the Application URL text box in cPanel's Node.js Selector is set to myapp:
const express = require('express');
const app = express();
app.get('/myapp/', function(req, res){
res.send("Hello from the root application URL");
});
app.get('/myapp/test/', function(req, res){
res.send("Hello from the 'test' URL");
});
app.listen(0, () => console.log('Application is running'));
In this code sample, two routes are defined,/myapp and /myapp/test. If your domain name is example.com, and you use your web browser to viewhttp://example.com/myapp or http://example.com/myapp/test, the pages load as expected. However, if you visit any other URL underhttp://example.com/myapp, you receive the Cannot GET error message.
Related Articles
Updated 1 day ago