javascript - 如何根据IP位置将用户重定向到子域?

标签 javascript node.js express subdomain

我的网站使用 Node Express,我想根据用户当前位置重定向用户,例如,如果新加坡的用户打开 site.com,则将他们重定向到子域 sg.site.com(该服务器与 site.com 托管在同一服务器上)。我能够找到用户的 IP 地址和位置,但如何将它们重定向到正确的子域?

最佳答案

如果您的意思是从服务器端 Express 代码,使用 res.redirect :

res.redirect([status,] path)

Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an HTTP status code . If not specified, status defaults to “302 “Found”.

res.redirect('/foo/bar');
res.redirect('http://example.com');
res.redirect(301, 'http://example.com');
res.redirect('../login');

如果您指的是客户端,请分配给 location object .

Replace the current document with the one at the given URL:

function goMoz() {   
   window.location = "http://www.mozilla.org";  
}   

关于javascript - 如何根据IP位置将用户重定向到子域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51416038/

相关文章:

javascript - 在 D3 Js Vertical Tree 中单击一次在父级下打开所有子级

javascript - 如果路径有效,Lodash 返回值数组

javascript - 如何为我所有的 Express 中间件设置默认的拒绝 promise 行为?

javascript - 表达动态路由来处理/a/b-c-d- :e-:f

node.js - Node Express 内容长度

javascript - 在构造一个集合后让 backbone.js 运行一个函数?

javascript - Django is_valid() 返回 false

javascript - 使用对象中定义的属性更改 CSS

node.js - bcrypt.compareSync 总是返回 false

javascript - 使用 webpack 将 dist 复制到 libs 目录