2014-04-15 43 views
-1

我想知道是否有可能讓我從html文件中獲取所需的所有腳本和樣式表路由(不使用瀏覽器所有Nodejs獲取所有需要的腳本和樣式表從一個頁面請求路由(無瀏覽器)

例如:

我跑index.html,網站需要./style.css,也需要./script.js和大量圖片。


我怎麼能知道什麼index.html需要的style.cssscript.js和圖像的路線。

另一個問題:當瀏覽器需要一些文件,瀏覽器請求服務器並等待響應時,瀏覽器是否像呈現HTML文件一樣工作?那麼有沒有一種方法可以使用nodejs來模擬操作而不使用瀏覽器?

+0

投票反對的人可以告訴我原因嗎? – LiJung

回答

1

你必須在你的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(__目錄名+ '/公共'));

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

NPM安裝明示

add following line of code in app.js 

變種快遞=要求( '表示') ,應用程式=快遞();

+0

對不起,這不是我一直在尋找的東西,我不想通過瀏覽器尋找路線。 – LiJung

相關問題