Node.js application error message: "Cannot GET" URL

This article discusses a problem that may occur when you try to run a Node.js application using cPanel's Node.js Selector.

Table of Contents

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”. 

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 view http://example.com/myapp or http://example.com/myapp/test, the pages load as expected. However, if you visit any other URL under http://example.com/myapp, you receive the Cannot GET error message.

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.