var html ='<!DOCTYPE html>\n'+
'<html>\n'+
' <head>\n'+
' <meta charset="utf-8" />\n'+
' <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">\n'+
' <title>foobunny rulez!</title>\n'+
' <link rel=\'stylesheet\' href=\'/styles/app.css\' />\n'+
' <script src="http://127.0.0.1:6789/scripts/lib/require.js"></script>\n'+
' </head>\n'+
' <body> \n'+
' <script> \n'+
' document.write("test");\n'+
' require([\'/scripts/config.js\']);\n'+
' require([\'/scripts/main.js\']);\n'+
' </script>\n'+
' </body>\n'+
'</html>';
var jsdom = require("jsdom");
jsdom.defaultDocumentFeatures = {
FetchExternalResources : ['script'],
ProcessExternalResources : ['script'],
MutationEvents : '2.0',
QuerySelector : false
}
var window = jsdom.jsdom(html, null, {
features: {
FetchExternalResources: ["script"],
ProcessExternalResources: ["script"]
}
}).createWindow();
window.addEventListener('load', function(){
setTimeout(function(){
res.send('<html>'+window.document.getElementsByTagName('html')[0].innerHTML)+'</html>';
}, 5000);
});
我有一小段代码,并且 require 确实最终将脚本标签添加到头部(它添加了<script type="text/javascript" charset="utf-8" data-requirecontext="_" data-requiremodule="/scripts/config.js" src="/scripts/config.js"></script>
<script type="text/javascript" charset="utf-8" data-requirecontext="_" data-requiremodule="/scripts/main.js" src="/scripts/main.js"></script>
)
但问题是它似乎没有在这些脚本中运行任何代码(其中包括更多的 require 语句)。当页面加载时,一切运行正常,但我正在尝试进行服务器端渲染。
我应该使用绝对路径吗?我尝试http://127.0.0.1:6789/scripts/main.js
对其他所有内容都使用和相同,但它似乎不起作用。有什么问题?