using switch in get request in express js is returning error why?

so I’m still a beginner in nodejs , and when I run http://127.0.0.1:3000/ in the browser I get this message, Cannot GET / I think, it is because I used a switch statement. can’t I use a switch inside app.get ? I want to make the code DRY that’s why I used a switch.

 const express = require('express');
        const app = express();
        const port = 3000;
        const hostname = 'localHost';
        app.get('/:main/:sub/:sub2', (req, res) => {
            let main = String(req.params.main).toLowerCase();
            let sub = String(req.params.sub).toLowerCase();
            let sub2 = String(req.params.sub2).toLowerCase();
            switch (main) {
                case "":
                    res.send(`Hi there welcome to my assignment`);
                case "speak":
                    {
                        switch (sub) {
                            case "pig":
                                res.send(`The pig says 'Oink'`);
                            case "cow":
                                res.send(`The cow says 'Moo'`);
                            case "dog":
                                res.send(`The dog says 'Woof Woof!'`);
                            case "*":
                                res.send(`sorry no one speaks here!`);
                        }
                    }
                case "repeat":
                    {
                        if (sub2 === !NaN) {
                            for (let i = 0; i < sub2; i++) {
                                res.send(`${sub}`);
                            }
                        }
                    }
                default:
                    res.send(`Page not found!`);
            }
        });
        app.listen(port, hostname, () => {
            console.log(`server has started`)
        });

Answer

Your empty string case isn’t a valid parameter to search for within main.

Create a default route using app.get instead:

 const express = require('express');
        const app = express();
        const port = 3000;
        const hostname = 'localHost';
        app.get("/", (req, res) => res.send("Welcome to my assignment"));
        app.get('/:main/:sub/:sub2', (req, res) => {
            let main = String(req.params.main).toLowerCase();
            let sub = String(req.params.sub).toLowerCase();
            let sub2 = String(req.params.sub2).toLowerCase();
            switch (main) {
                case "speak":
                    {
                        switch (sub) {
                            case "pig":
                                res.send(`The pig says 'Oink'`);
                                break;
                            case "cow":
                                res.send(`The cow says 'Moo'`);
                            break;
                            case "dog":
                                res.send(`The dog says 'Woof Woof!'`);
                            break;
                            case "*":
                                res.send(`sorry no one speaks here!`);
                            break;
                        }
                    }
                case "repeat":
                    {
                        if (!Number.isNaN(sub2)) {
                            let str = "";
                            for (let i = 0; i < sub2; i++) {
                                str+=sub;

                            }
                            res.send(str);
                        }
                        break;
                    }
                default:
                    res.send(`Page not found!`);
                    break;
            break;
            }
        });
        app.listen(port, hostname, () => {
            console.log(`server has started`)
        });

Edit I also adjusted the “repeat” functionality to work. !NaN is always true. Your original code basically said if(sub2 === true) which it never would be. I modified it using the !Number.isNaN conditional method. I then concatenated a string and output it n number of times.

Leave a Reply

Your email address will not be published. Required fields are marked *