[英]Nodejs getting all needed scripts and stylesheets route from a page request(without browsers)
我想知道是否有可能從 html 文件中獲取我需要的所有腳本和樣式表路由。(根本不使用瀏覽器。 )
例如:
我運行index.html
,網站需要./style.css
,還需要./script.js
和大量圖像。
我怎么知道index.html
需要style.css
和script.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.