Node.js Error: Cannot GET/ from Running the URL on the Web Browser
Last Updated :
02 Aug, 2024
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.
Similar Reads
How to Show the Line which Cause the Error in Node.js ?
Debugging is a critical part of software development. When an error occurs in a Node.js application, understanding exactly where it happened is essential for diagnosing and fixing the problem. Node.js provides several ways to pinpoint the line of code that caused an error. This article explores thes
4 min read
How to Fix the âNODE_ENV is not recognizedâ Error in Node JS
We generally come across the error message "NODE_ENV is not recognized as an internal or external command, operable command, or batch file" while trying to set an environment variable in a package.json script in Node JS. This guide will assist you in resolving the issue with a straightforward soluti
2 min read
How to check whether a script is running under Node.js or not ?
In JavaScript, there is not any specific function or method to get the environment on which the script is running. But we can make some checks to identify whether a script is running on Node.js or in the browser. Using process class in Node.js: Each Node.js process has a set of built-in functionalit
3 min read
How to resolve a "Cannot find module" error using Node.js?
This article outlines the steps you can take to troubleshoot and fix "Cannot find module" errors in your Node.js projects. These errors typically arise when Node.js cannot locate a module you're trying to use in your code. Table of Content Error "Cannot find module" Approach to Solve the ErrorInstal
3 min read
How to Pass Node.js Output to Web Interface ?
Node.js is a versatile runtime that excels at building server-side applications. One of the common use cases is to pass the output from a Node.js server to a web interface, allowing users to interact with server-generated data in a browser. This article will walk you through the process of passing N
2 min read
Reading Environment Variables From Node.js
Environment Variable: The two fundamental concepts of any programming language are variables and constants. As we know that constants and variables both represent the unique memory locations that contain data the program uses in its calculations. The variable which exists outside your code is a part
2 min read
How to run ExpressJS server from browser ?
ExpressJS is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Typically, ExpressJS servers are run on a Node.js environment on the backend. However, for development, testing, or educational purposes, you might want to ru
2 min read
How to Get the Path of Current Script using Node.js ?
In Node JS, getting the path of the current script is useful for file operations, logging, and configuration. It allows you to access related files or directories reliably, regardless of where your Node.js process was started. ApproachTo get the path of the present script in node.js we will be using
2 min read
How to Create and Run a Node.js Project in VS Code Editor ?
Visual Studio Code (VS Code) is a powerful and user-friendly code editor that is widely used for web development. It comes with features like syntax highlighting, code suggestions, and extensions that make coding easier. In this article, we'll show you how to quickly create and run a Node.js project
2 min read
How to Deal with CORS Error in Express Node.js Project ?
Cross-Origin Resource Sharing (CORS) errors occur in ExpressJS applications when a web page attempts to make requests to a domain different from the one that served it, and the server hasn't been configured to allow such requests. CORS errors are common in NodeJS projects when working with APIs. Com
5 min read