简体   繁体   English

关于使用 Node.js“代理”服务器代理 CSS 和 JS 的建议

[英]Advice on proxying CSS and JS with a Node.js "proxy" server

I have a working server for entering a URL in a query parameter, it fetches the page and sends it back.我有一个用于在查询参数中输入 URL 的工作服务器,它获取页面并将其发送回。 The problem is, the CSS links don't always work because generally CSS is fetched from the source by the HTML <link rel="stylesheet"> tag.问题是,CSS 链接并不总是有效,因为通常 CSS 是通过 HTML <link rel="stylesheet">标签从源获取的。 Is there a way I can fetch the CSS from the URL and send it in a <style> tag?有没有办法可以从 URL 中获取 CSS 并将其发送到<style>标签中?

I'd like to be able to do this with JavaScript as well, but it is not a requirement.我也希望能够使用 JavaScript 执行此操作,但这不是必需的。

Additionally, as a "nice-to-have", is it possible to change anchor href tags to change the query parameter instead of URL itself?此外,作为“不错的选择”,是否可以更改锚点 href 标签以更改查询参数而不是 URL 本身? privatedomain.com/page?url=<urlhere> is the URL structure I have, and would like it to be able to redirect from something like privatedomain.com/page?url=http://www.google.com to privatedomain.com/page?url=http://www.google.com/search?q=hmm+yes privatedomain.com/page?url=<urlhere>是我拥有的 URL 结构,并且希望它能够从诸如privatedomain.com/page?url=http://www.google.com类的内容重定向到privatedomain.com/page?url=http://www.google.com/search?q=hmm+yes

Really just a thing I'm doing for fun and trying to get it to work, if I asked this question incorrectly, I'm sorry, it's my first time asking here, thanks!真的只是我为了好玩而做的一件事,并试图让它发挥作用,如果我问错了这个问题,对不起,这是我第一次在这里问,谢谢!

Parse the document, find <link rel="stylesheet"> , extract the relative path to CSS from there.解析文档,找到<link rel="stylesheet"> ,从那里提取 CSS 的相对路径。

Then you can concatenate the relative path with the root of your <urlhere> .然后,您可以将相对路径与<urlhere>的根连接<urlhere> Then replace a link inside <link rel="stylesheet"> with your combined path.然后将<link rel="stylesheet">的链接替换为您的组合路径。

Example: privatedomain.com/page?url= http://example.com/foo/bar.html示例:privatedomain.com/page?url= http://example.com/foo/bar.html

in foo.html you have <link rel="stylesheet" href="/styles.css">foo.html你有<link rel="stylesheet" href="/styles.css">

You need to make const absoulteCssPath = 'http://example.com' + '/styles.css' ;你需要让const absoulteCssPath = 'http://example.com' + '/styles.css' ; and then replace /styles.css inside <link rel="stylesheet" href="/styles.css"> with your absoulteCssPath .然后替换/styles.css内部<link rel="stylesheet" href="/styles.css">absoulteCssPath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM