使用Express将javascript代码注入html
我正在使用使用Express将javascript代码注入html,javascript,html,express,impress.js,Javascript,Html,Express,Impress.js,我正在使用Express框架编写一个web应用程序,以生成Impress.js演示文稿并对其应用可视化编辑器。 我已经设置了app.js,只用于获取.html文件,但是我想在.html文件的body closing标记之前插入一个单独文件中的javascript代码 例如,我将有一个路由处理程序/edit,然后脚本将在注入html代码后被触发 var express = require('express'); var app = express(); app.configure(funct
Express
框架编写一个web应用程序,以生成Impress.js
演示文稿并对其应用可视化编辑器。
我已经设置了app.js
,只用于获取.html文件,但是我想在.html文件的body closing标记之前插入一个单独文件中的javascript代码
例如,我将有一个路由处理程序/edit
,然后脚本将在注入html代码后被触发
var express = require('express');
var app = express();
app.configure(function(){
app.use(express.static(__dirname + '/public'));
});
app.get('/:file', function(req, res) {
res.sendfile(__dirname + '/public' + file);
});
app.listen(3000);
console.log('Listening on port 3000');
任何关于如何实现这一点的想法都是非常受欢迎的 这只是一个想法,但您可以使用类似于将脚本附加到正文的内容
app.get('/:file', function(req, res) {
var html = fs.readFileSync(__dirname + '/public' + file, 'utf8');
var $ = cheerio.load(html);
var scriptNode = '<script>alert("script appended!");</script>';
$('body').append(scriptNode);
res.send($.html());
});
app.get('/:file',函数(req,res){
var html=fs.readFileSync(uu dirname+'/public'+文件'utf8');
var$=cheerio.load(html);
var scriptNode='alert(“脚本追加!”);';
$('body').append(scriptNode);
res.send($.html());
});
它可以工作!但是现在已经嵌入到.html中的任何其他脚本都不会执行。我怎样才能解决这个问题?谢谢,你说他们没有被处决是什么意思?它把它们拿走了吗?有错误吗?请详细说明我何时执行console.log($.html());我可以在html中看到脚本,但它们不像附加的scriptNode那样被触发。我在描述中的代码没有这个问题。如果你说加载时脚本没有在页面上运行,你需要查看浏览器控制台,看看是否有任何客户端js错误。你是对的,我忘了再次配置express.static server。再次感谢你的帮助!