What does it mean?
A Node.js app uses routes. A route is a rule that says "when someone visits this address, send this answer". Think of a shop with signs on each door. If you walk to a door with no sign, nobody answers. "Cannot GET /" means the app is running, but it has no route for that address. GET is the kind of request a browser makes when it asks for a page.
Common causes
- No route is set for the path, such as
/or/about. - The route is written after another rule that catches everything.
- A typing mistake in the path.
- The app uses the wrong method, for example
app.postinstead ofapp.get. - Static files such as
index.htmlare not served. - The wrong folder or app is running.
Steps to fix it
- Open your main file, such as
app.jsorserver.js. - Look for the route for the path you opened. For the home page, you need something like this:
app.get('/', (req, res) => {
res.send('Hello from my app');
});
This sends a message when someone opens the home page.
- If you want to show an HTML file, serve a folder of files. This example uses a folder called
public:
app.use(express.static('public'));
This tells Express, a common Node.js tool, to share the files inside public. Put your index.html there.
- Check that the route uses
app.getfor pages that a browser opens. - Check the spelling and the letter case of the path.
/Aboutand/aboutcan differ. - If you use a router file, make sure it is added to the app:
app.use('/', require('./routes/index'));
This connects the routes in that file to your app.
- Stop the app and start it again so it loads your changes:
node app.js
If you use PM2, run pm2 restart all.
- Open the address in your browser again.
On a hosting panel
If you use a Node.js app tool in your control panel, check that the application root and startup file point to the right folder and file. Then restart the app from the panel. If you are unsure, open a support ticket.
Quick recap
- "Cannot GET" means no route matches the address.
- Add or fix a route with
app.get. - Serve static files with
express.static. - Restart the app after every change.