Skyler827
Skyler827

Reputation: 107

Express won't return HTTP response from one controller, but does in others

Thanks for looking into my question.

I have a node/express server, configured with a server.js file, which calls urls.js which in turn calls controllers for handling http requests, and all of them are configured identically and all work fine, except for one.

It's my OrderController. The OrderController.js never gets called, and thus never returns any HTTP responses even though I'm as sure as I can be that it should be getting called.

I've tried adding logging statements all over OrderController.js, but they never fire. Nothing in my OrderController.js is running at all.

I've inspected my urls.js file carefully but can find no clue why OrderController would fail to load, while my other 8 controllers all work fine.

I've added logging statements to my server.js file to ensure that my server.js file is indeed handling all http requests and passing them correctly to my urls.js file.

I've added logging statements to my urls.js file to confirm that it is being executed.

I've logged the controllers, both require(OrderController.js) and my other controllers, and inspected the dumped json objects, but couldn't determine anything different or mistaken from the output I got.

The repository is: https://github.com/allenchan3/foodproject/tree/40a43231ae49989a35fb0c004a897bbee69fe669

In theory, the problem should be somewhere in urls.js:

const path = require("path");
const staticFilesError = require("./errors").errorHTML;
module.exports = function(app, staticDir) {
    const loginRegController = require(path.join("..","controllers","LoginRegController"));
    const controllers = {
        'categories': require(path.join("..","controllers","CategoryController")),
        'diningrooms':require(path.join("..","controllers","DiningRoomController")),
        'ingredients':require(path.join("..","controllers","IngredientController")),
        'items':      require(path.join("..","controllers","ItemController")),
        'options':    require(path.join("..","controllers","OptionController")),
        'orders':     require(path.join("..","controllers","OrderController")),
        'tables':     require(path.join("..","controllers","TableController")),
        'users':      require(path.join("..","controllers","UserController")),
    };

    for (key in controllers) {
        app.use("/api/"+key, controllers[key]);
    }
    app.use(loginRegController);
    app.get(/^\/api\//, (req, res) => res.status(404).json({"error":req.url+" not found"}));
    app.get("*", (_,res) => res.sendFile(staticDir+"/index.html"));
}

I am just trying to make HTTP GET requests to /api/orders/ and get something from the order controller (currently it should get a list of all orders from my database). I also made a handler for /hello so /api/orders/hello should be at least returning something. Whats actually happening is no http response ever comes, not even a 404, and I don't have any idea why.

Upvotes: 0

Views: 587

Answers (1)

Alex
Alex

Reputation: 36

I cloned your repo and was able to reproduce the bug.

What is seems to be is that in authentication.js, the handleOrders() function only handles a user being a manager, bartender, server or cashier. If the req.session.userType is not set or it its equal to cook or customer, it is not handled by the if statement and will hang indefinitely.

Can you attempt to call the API as a user with a different type and confirm this?

You may need to an else as below to throw a 403 if the user is the wrong type.

function handleOrders(req, res, next) {
    console.log('handling')
    if (req.session.userType == "manager") {
        console.log('i am manager')
        next();
    } else if (["bartender","server","cashier"].indexOf(req.session.userType) > -1) {
        console.log('am i a cahsier or server or bartender')
        if (req.url == "/api/order" && req.method == "GET") {
            console.log('in this if')
            next();
        }
        else {
            //not a get
            Order.findById(req.params.id,(err,data)=>{
                if (err) {
                    console.log(err);
                    res.status(500).json(err);
                } else {
                    console.log(data);
                    next();
                }
            });
        }
    } else res.status(403).send(errors.forbidden);

}

Upvotes: 2

Related Questions