Search code examples
node.jsfirebaseexpressfirebase-hostingfirebase-cli

Why does add firebase serve command some additional info to url?


My problem is about testing my codes on localhost (ports: 5000, 5001). I'm using the command below in terminal windows:

sudo firebase serve

And it's giving the links after ready:

hosting[helloapp]: Serving hosting files from: public
hosting[helloapp]: Local server: http://localhost:5000
functions: app: http://localhost:5001/helloapp/us-central1/app

It seems ok. But when I try the functions some url doesn't work correctly. For example: if there is a redirect code in backend path of the url disappeared, 'helloapp/us-central1/app'. Hosting url working nice.. I dont know why the functions:app URL have some additional part?

Backend index.js

app.use('/', express.static(path.join(__dirname, '../public'))); 
app.set('view engine', 'ejs'); 
app.set('views', path.join(__dirname, './app_server/views')); 

Redirect code:

catch(error => {
  console.log('cant access', error);
  res.redirect('/login');
}    

The Problem

The URL before redirect:

http://localhost:5001/helloapp/us-central1/app

The URL after redirect:

http://localhost:5001/login

The URL, I expect

http://localhost:5001/helloapp/us-central1/app/login

Solution

  • Partial URLs that start with a slash are interpreted to be path that's relative to the host of the original URL. These are sometime called "absolute path reference". They effectively erase the entire path of the url and start over with the path you've given.

    (read more about that)

    If you want your redirect URL to build off the existing path, you could simply append the path parts that you need to the original path (found in req.path)

    res.redirect(req.path + '/login');
    

    This should add a path component to the end of the existing URL rather than erasing the existing path.