node.jssqliteexpresshttp-errorerror-messaging

Troubleshooting ‘Cannot GET /’ Error when running ‘Example Backend’ in Node


I am trying to run the ‘Example Backend’ from the Codecademy 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? how I can fix this?

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"
    }
}

server.js

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

// Initialize the express app
const app = express();
// Use the body-parser middleware to parse the request body as json
app.use(bodyParser.json());

// Initialize the database
const db = initDB();

// Route to retrieve all users
app.get("/users", (req, res) => {
    // Select all users from the 'users' table
    db.all("SELECT * FROM users", [], (err, rows) => {
        if (err) {
            // If there is an error, send a 500 status code with the error message
            res.status(500).json({"error": err.message});
        } else {
            // Otherwise, send the rows as the response
            res.json({users: rows})
        }
    });
});

// Route to retrieve a specific user by id
app.get("/users/:id", (req, res) => {
    // Get the id from the request params
    const { id } = req.params;
    // Select the user with the specified id from the 'users' table
    db.all("SELECT * FROM users where id is (?)", [id], (err, rows) => {
        if (err) {
            // If there is an error, send a 500 status code with the error message
            res.status(500).json({"error": err.message});
        } else if (rows.length === 0) {
            // If no user is found, send an empty object as the response
            res.json({user: {}})
        } else {
            // Otherwise, send the first row as the response
            res.json({user: rows[0]})
        }
    })
});

// Route to create a new user
app.post("/users", (req, res) => {
    // Get the username and password from the request body
    const { user: { username, password} } = req.body;
    // The insert statement for the 'users' table
    const insertStmt = "INSERT INTO users(username,password) VALUES (?,?)";
    db.run(insertStmt, [username, password], function(err, result) {
        if (err) {
            // If there is an error, send a 500 status code with the error message
            res.status(500).json({ "error": err.message });
        } else {
            // Otherwise, send the newly created user object as the response
            res.json({
                id: this.lastID,
                username,
                password
            })
        }
    })
});

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

db.js

const sqlite3 = require("sqlite3");
// Address for the in-memory database
const DB_ADDR = ":memory:";

// Sample data for the 'users' table
const users = [
    {
        username: "1lameuser",
        password: "secret_password"
    },
    {
        username: "cool_user_87",
        password: "notPassword!"
    },
];

/**
 * Initializes the database with the 'users' table and sample data
 */
const initDB = () => {
    // Create a new in-memory database
    const db = new sqlite3.Database(DB_ADDR);
    // Serialize the database operations to prevent concurrent writes
    db.serialize(() => {
        // Create the 'users' table
        db.run(`CREATE TABLE users (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            username TEXT UNIQUE,
            password TEXT
        )`);

        // Prepare the insert statement for the 'users' table
        const insertStmt = db.prepare("INSERT INTO users(username,password) VALUES (?,?)");
        // Insert the sample data into the 'users' table
        users.forEach(({ username, password}, i) => {
            insertStmt.run([username, password]);
        })
        // Finalize the insert statement
        insertStmt.finalize();
    });
    return db;
};

module.exports = { initDB };


Solution

  • You have defined 3 routes in your Express app:

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

    When the server gets a request, it tries to match the request HTTP method (for example POST\GET) and the request path (for example /users/:id) to a route.

    As you can see, there is no route that matches a request with the HTTP method GET and path /, and that's why you're getting an error.

    To test it, try adding the following route to your App and call it again. This route will match a GET request with a path /

    app.get('/', (req, res) => {
       res.sendStatus(200)
    });