Introduction
This article has a specific content on solving an error message. The detail of the error message exist in the title of this article. It is an error message of ‘Cannot Get /’ exist in the page of NodeJS application. Actually, the error appear upon executing a NodeJS application. Running the application by executing a command in the command line interface as follows :
[admin@10 nodejs]$ node app.js Test NodeJS !
But upon accessing the page via web browser, the following is the image of the page after successfully executing the NodeJS application :
Actually, the code has a part for listening to a specific port. In the context of this article, the application is listening to port 3001. The following is the actual content of the source code with the file name of ‘app.js’ :
const express = require("express"); const app = express(); app.get("/"); app.listen(3001); console.debug("Test NodeJS !");
So, while printing the ‘Test NodeJS !’ in the command line interface, it also listens for incoming request in port 3001. But apparently, accessing port 3001 via web browser fail with the error message as in the above image.
Solving the problem
In order to solve the problem, just fix the above line of code especially in the following line :
app.get("/");
Upon executing a GET request by typing the address of ‘localhost:3001’ in the web browser’s address bar, the above image appear. The error message is ‘Cannot Get /’. It is because there is no definition for responding the value from the line of ‘app.get(‘/’)’. In order to a page retrieving a responding value, just define it in the line of the code. The following is one of the method for defining at responding value to the GET request as follows :
app.get("/",(req,res) => res.send("Response from the GET request"));
Another form of definition with the full-length syntax of the function exist as follows :
app.get("/", function (req,res) { res.send("Response from the GET request") });
So, the full content of the file with the name of ‘app.js’ exist as follows :
const express = require("express"); const app = express(); app.get("/",(req,res) => res.send("Response from the GET request")); app.listen(3001); console.debug("Test NodeJS !");
Or another form of the function definition displaying the full content of the file :
const express = require("express"); const app = express(); app.get("/", function (req,res) { res.send("Response from the GET request") });app.listen(3001); console.debug("Test NodeJS !");