Troubleshooting 'Cannot GET /' Error when running 'Example Backend' in Node

I am trying to run the ‘Example Backend’ from the course ‘Setting Up Postman - Learn Express Routes’ as part of my path as a Back-End Engineer. However, when I try to access it in my browser, I get an error message that says, “Cannot GET /.” My node version is v16.19.0, and I have already installed npm and Espress. Has anyone else encountered this error?

package.json

{
    "name": "example_backend",
    "version": "1.0.0",
    "description": "A simple backend server to test out API requests",
    "main": "server.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "Codecademy",
    "license": "ISC",
    "dependencies": {
        "body-parser": "^1.20.1",
        "express": "^4.18.2",
        "sqlite3": "^5.1.4"
    }
}

Hey!

Could you share the server code that’s running? It may be that there is no route set up for the / using GET, but that doesn’t necessarily mean that the rest of the server isn’t working as it should :slight_smile:

1 Like

Hello!
Thank you for responding to this message.
Below is the code:

server.js

const express = require('express');
const bodyParser = require('body-parser');
const { initDB } = require('./db');

const app = express();
app.use(bodyParser.json());

const db = initDB();

app.get("/users", (req, res) => {
    db.all("SELECT * FROM users", [], (err, rows) => {
        if (err) {
            res.status(500).json({"error": err.message});
        } else {
            res.json({users: rows})
        }
    });
});

app.get("/users/:id", (req, res) => {
    const { id } = req.params;
    db.all("SELECT * FROM users where id is (?)", [id], (err, rows) => {
        if (err) {
            res.status(500).json({"error": err.message});
        } else if (rows.length === 0) {
            res.json({user: {}})
        } else {
            res.json({user: rows[0]})
        }
    })
});

app.post("/users", (req, res) => {
    const { user: { username, password} } = req.body;
    const insertStmt = "INSERT INTO users(username,password) VALUES (?,?)";
    db.run(insertStmt, [username, password], function(err, result) {
        if (err) {
            res.status(500).json({ "error": err.message });
        } else {
            res.json({
                id: this.lastID,
                username,
                password
            })
        }
    })
});

app.listen(4000, () => console.log("Simple server running on http://localhost:4000"))

db.js

const sqlite3 = require("sqlite3");

const DB_ADDR = ":memory:";

const users = [
    {
        username: "1lameuser",
        password: "secret_password"
    },
    {
        username: "cool_user_87",
        password: "notPassword!"
    },
];

const initDB = () => {
    const db = new sqlite3.Database(DB_ADDR);
    db.serialize(() => {
        db.run(`CREATE TABLE users (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            username TEXT UNIQUE,
            password TEXT
        )`);

        const insertStmt = db.prepare("INSERT INTO users(username,password) VALUES (?,?)");
        users.forEach(({ username, password}, i) => {
            insertStmt.run([username, password]);
        })
        insertStmt.finalize();
    });
    return db;
};

module.exports = { initDB };

Yep that’s why, you have the following routes set up:

GET /users
GET /users/:id
POST /users

When visiting the base localhost:4000 URL in the browser, you’re sending a GET request to /. But there’s no route defined for a GET request to /, which is why visiting the URL in the browser throws an error :slight_smile:

The server is still running as it should, and any of the defined routes should work fine! Try visiting localhost:4000/users to test.