gpt4 book ai didi

javascript - 在 JavaScript 中获取我的 Web 应用程序基本 URL

转载 作者:数据小太阳 更新时间:2023-10-29 03:59:16 26 4
gpt4 key购买 nike

如何在 Node.js 中获取我的 Web 应用程序 URL?我的意思是如果我的站点基本 url 是 http://localhost:8080/MyApp我怎么能得到它?

谢谢,

最佳答案

你必须连接 'url' 模块

var http = require('http');
var url = require('url') ;

http.createServer(function (req, res) {
var hostname = req.headers.host; // hostname = 'localhost:8080'
var pathname = url.parse(req.url).pathname; // pathname = '/MyApp'
console.log('http://' + hostname + pathname);

res.writeHead(200);
res.end();
}).listen(8080);

更新:

在 Node.js v8 url 模块中获取用于处理 URL 的新 API。参见 documentation :

Note: While the Legacy API has not been deprecated, it is maintained solely for backwards compatibility with existing applications. New application code should use the WHATWG API.

关于javascript - 在 JavaScript 中获取我的 Web 应用程序基本 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10251805/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com