简体   繁体   中英

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

I'm wondering if it is possible for me to get all the scripts and stylesheets routes I need from a html file.( not using browsers at all. )

for example:

I run index.html , and the website need ./style.css and also need ./script.js and lot of images.


How could I know what index.html need the routes of style.css and script.js and images.

another question: Does the browser works like rendering HTML file while it need some files, the browser request to server and wait for response? So is there a way I could use nodejs to analog the action and not using browsers?

You have to specify the location of your static content in your app.js file from where server will serve the files to browser when index.html will render.

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 install express

 add following line of code in app.js

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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