相当于NodeJS中的window.location和window.pathname [英] Equivalent of window.location and window.pathname in NodeJS

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

问题描述

NodeJS/ExpressJS中的window.location.protocolwindow.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.location和window.pathname的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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