NodeJS中的window.locaton和window.pathname的等价物 [英] Equivalent of window.locaton and window.pathname in NodeJS

查看:99
本文介绍了NodeJS中的window.locaton和window.pathname的等价物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NodeJS / ExpressJS中的 window.location.protocol window.location.host 相当于什么?

What is the equivalent to window.location.protocol and window.location.host in NodeJS/ExpressJS?

我正在尝试使用第三方API将网址重定向回我的网站。

I'm trying to redirect the url back to my site using a third party API.

推荐答案

NodeJS API url.format(urlObject)方法返回源自 urlObject 的格式化URL字符串。

The NodeJS API url.format(urlObject) method returns a formatted URL string derived from urlObject.

var url = require('url');

function getFormattedUrl(req) {
    return url.format({
        protocol: req.protocol,
        host: req.get('host')
    });
}

res.redirect(getFormattedUrl(req));

这篇关于NodeJS中的window.locaton和window.pathname的等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆