[英]Inject javascript code into html with Express
I am writing a web application using Express
framework to generate Impress.js
presentations and apply a visual editor on them. 我正在使用
Express
框架编写一个Web应用程序来生成Impress.js
演示文稿并在其上应用可视化编辑器。 I have set up the app.js
for only getting the .html file but I want to inject a javascript code from a separate file before the body closing tag of the .html file. 我已经设置
app.js
只获取.html文件,但我想在.html文件的正文结束标记之前从单独的文件中注入一个javascript代码。
For example, I will have a route handler /edit
and then the script will be triggered after it have been injected into the html code. 例如,我将有一个路由处理程序
/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');
Any ideas on how to achieve this would be more than welcome! 关于如何实现这一目标的任何想法都将受到欢迎!
Just an idea, but you could use something like cheerio to append a script to the body 只是一个想法,但你可以使用像cheerio这样的东西来附加一个脚本到身体
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());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.