Node.js Error: Cannot GET/ from Running the URL on the Web Browser

The “Cannot GET /” error in Node.js typically occurs when you try to access a route in your web browser that hasn’t been defined in your application. This common issue can be resolved by ensuring that your server is correctly set up to handle incoming requests at the root path or other specified paths.

Understanding the Error

When you see the “Cannot GET /” error, it means that the server received a request for the root URL (/), but there is no route defined to handle this request. In a Node.js application, routes define how an application responds to client requests at a particular endpoint.

Steps to Set Up Project

Step 1: Initializes NPM

Create and Locate your project folder in the terminal & type the command

npm init -y

It initializes our node application & makes a package.json file.

Step 2: Install the required library

necessary packages/libraries in your project using the following commands.

npm install express

Step 3: Create Server File

Create an ‘app.js’ file, inside this file require an express Module, and create a constant ‘app’ for creating an instance of the express module.

const express = require('express')
const app = express()

Step 4: Create a Message Route

Create a get route using app.get() method which sends “hello” as a text to the web browser.

app.get("/messages", (req, res) => {
res.send("Hello");
});

Step 5: Set up a port to run our server

We will do it by using the express, app.listen() method.

app.listen(3000, () => {
console.log("listening on http://localhost:3000");
})

Example: Implementation to show how the above error occurs.

Node
const express = require('express')
const app = express()

app.get("/messages", (req, res) => {
    res.send("Hello");
});

app.listen(3000, () => {
    console.log("listening on http://localhost:3000");
})

Step to Run Application: Run the application using the following command from the root directory of the project

node index.js

Output: Your project will be shown in the URL http://localhost:3000/

Reason:

Since in the server file, we create a get route for ‘/messages’ URL but inside the browser, we try to get the ‘/’ URL which is not specified in our server file that’s why it throws the error.

Solution Approach:

We have to set up a universal route, and when any route or URL which are not specified inside the server file will call then the universal URL sends a “404 URL NOT FOUND” message.

app.get("/:universalURL", (req, res) => {
res.send("404 URL NOT FOUND");
});

Example: Implementation to show how to resolve the above error occured.

Javascript
const express = require('express')
const app = express()

app.get("/messages", (req, res) => {
    res.send("Hello");
});

app.get("/:universalURL", (req, res) => {
    res.send("404 URL NOT FOUND");
});

app.listen(3000, () => {
    console.log("listening on http://localhost:3000");
})

Step to Run Application: Run the application using the following command from the root directory of the project

node index.js

Output: Your project will be shown in the URL http://localhost:3000/

Conclusion

The “Cannot GET /” error in Node.js is a common issue that arises when the server does not have a route defined for the root URL or the requested path. By following the steps outlined in this guide, you can define the necessary routes, serve static files, and handle undefined routes to ensure your Node.js application responds correctly to incoming requests. This approach helps in building robust and user-friendly web applications.



Contact Us