0

I might be wording the question incorrectly...

I'm trying to make a url shortener using Node.js, Express, MongoDB (mongoose).

I've set it so that when the user loads the home page, they are redirected to the home page.

// horribly designed home page
app.get('/', function(req, res){
    res.render('index');
    res.end();
});

This part works. The user successfully sees the home page.

But then, I also have the following code:

// redirects to corresponding url
app.get('/:digits', function(req, res){
    console.log('getting full url from database');
}

Every time the user navigates to the home page (/), this path is also triggered and I see the 'getting full url from database' in the console.

Why is this?

Full code is here

dikesh
  • 2,977
  • 2
  • 16
  • 26
Human Cyborg Relations
  • 1,202
  • 5
  • 27
  • 52
  • Browser is sending request for `/favicon.ico` and hence the `/:digits` path is also triggered. Send curl request to your home page like `curl http://localhost:3000/` and you will see that `/:digits` route is not triggered. – dikesh Dec 17 '16 at 05:58

2 Answers2

1

Check out this section on Route parameters:

http://expressjs.com/en/guide/routing.html From the official documentation:

This request URL:

http://localhost:3000/users/34/books/8989

will be routed to this path:

/users/:userId/books/:bookId

and the req.params object will be

{ "userId": "34", "bookId": "8989" }

In your case: when I tried it in chrome I got req.params.digits as 'favicon.ico' .

Which means the browser is making two requests:

  1. to '/' path
  2. to '/favicon.ico'

The second request is why the route is triggered.

You can try adding console.log code to see what your browser is sending

app.get('/:digits', function(req, res){
    console.log('getting full url from database');
    console.log(req.params.digits);
});

You can also use 'developer options' (Chrome), firebug (Firefox) to verify.

Rayee Roded
  • 2,440
  • 1
  • 20
  • 21
0

I'm looking through your code, and I think you may be missing a couple of things.

Do you have a 'views' folders setup? By using res.render, express is expecting to render a 'view' from the 'views' folder with the 'ejs' view engine.

I've found that without having that folder explicitly defined within my structure to be a problem when it comes to routing.

Brian Zhou
  • 574
  • 6
  • 15
  • Yes, I do have a views folder with index.ejs inside. Sorry, should have mentioned that in the original post – Human Cyborg Relations Dec 17 '16 at 02:29
  • Not sure if this helps... but have you tried commenting out the res.end() after your res.render() statement? – Brian Zhou Dec 17 '16 at 02:40
  • Not sure if this helps... but have you tried commenting out the res.end() after your res.render() statement? Not sure if it will help... but I'd also check to see how you are rendering your static files. i.e. app.use('/static', express.static(path.join(__dirname, 'public'))) https://expressjs.com/en/starter/static-files.html – Brian Zhou Dec 17 '16 at 02:42