繁体   English   中英

Nodejs 从页面请求中获取所有需要的脚本和样式表路由(无浏览器)

[英]Nodejs getting all needed scripts and stylesheets route from a page request(without browsers)

我想知道是否有可能从 html 文件中获取我需要的所有脚本和样式表路由。(根本不使用浏览器。

例如:

我运行index.html ,网站需要./style.css ,还需要./script.js和大量图像。


我怎么知道index.html需要style.cssscript.js和图像的路由。

另一个问题:当浏览器需要一些文件时,浏览器是否像渲染 HTML 文件一样工作,浏览器请求服务器并等待响应? 那么有没有一种方法可以使用 nodejs 来模拟动作而不使用浏览器?

您必须在 app.js 文件中指定静态内容的位置,当 index.html 呈现时,服务器将从该位置向浏览器提供文件。

for example if i have following directory structure

ROOT
 |
  ---public
 |     |
 |      ----css
 |     |     |
 |     |      ----style.css
 |     |     |
 |     |
 |      ----js
 |     |     |
 |     |      ----script.js
 |     |     |
 |     |
 |      ----images
 |     |
 |
 |
  ---app.js
 |
  ---index.html
 |

 Browser requested urls format are 
 css - > <link href="/css/style.css" rel="stylesheet" type="text/css" />
 js  - > <script type="text/javascript" src="/js/script.js"></script>

 after that you have to add following code in your app.js file;

app.use(express.static(__dirname + '/public'));

 if express is not installed in your system install it form cmd using command 

npm 安装快递

 add following line of code in app.js

var express = require('express') , app = express();

找出解决问题的好方法是使用 phantomjs( http://phantomjs.org/ ),网络监控可能是我正在寻找的东西。

暂无
暂无

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

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