拥有像 facebook.com/USERNAMEHERE 这样的用户配置文件的正确方法是什么?我目前正在使用 domain.com/users.html?USERNAMEHERE



我希望每个用户名都有URL。然而,我知道如何做到这一点的唯一方法是使用搜索查询(?问号(。例如,在Node.js中,我得到了"location.search"标记,它返回"?"之后的任何内容在www.domain.com/users.html?用户名。但是,我想把它放在可以使用www.domain.com/users/USERNAME.的地方

我正在使用Firebase和Node.js

这取决于您如何生成内容。

脑海中浮现的几种方法:

  • 从node.js中的URL获取用户名
  • 在firebase托管中使用重写

方法1:

如果您使用某种服务器端渲染,您只需从URL中获取用户名并动态地提供内容。示例:

app.get('/user/:username', function(req , res){
res.send("This is the profile for: " + req.params.username);
});

方法2:

如果你为每个用户提供相同的HTML文件,并在客户端上用用户数据填充它,你可以在firebase托管配置中使用重写:

{
"hosting": {
"rewrites": [
{
"source": "/user/*",
"destination": "/user/index.html"
}
]
}
}

这将重写domain.com/user/username/domain.com/user/index.html

注意:它不会重写domain.com/user/username/otherpage/。要做到这一点,您需要添加重写:

{
"source": "/user/*/otherpage/",
"destination": "/path/to/otherpage/"
}

注意:如果您使用此方法,请注意404错误,如果您使用相对路径引用资源。例如,假设这是/user/:中的一个HTML文件

<!-- inside of /user/index.html -->
<script src="javascript.js"></script>

此路径将是/users/username/javascript.js,并且可能返回index.html,这是一个问题。要解决此问题,请使用绝对路径:

<!-- inside of /user/index.html -->
<script src="/user/javascript.js"></script>

相关内容

最新更新