我想在我的网站上使用 on-click
事件执行一些特定的 node
JS
文件。
我正在使用 express
来运行我的服务器和网站。我认为正确的方法是使用 jQuery
和一些 GET
请求。 JS 文件正在运行,如果我在控制台中使用 "node examplefile.js"
文件看起来像这样:
var MapboxClient = require('mapbox');
var client = new MapboxClient('');
client.listStyles(function (err, styles) {
console.log(styles);
});
我想在每次 on-click
事件发生时执行这个文件。
我必须将其作为模块导出到我的 app.js
吗?这是我想做的,但我没有实现。
关于如何实现这一点有什么建议或简单的例子吗?
最佳答案
1.在函数中编写 examplefile.js
function a(){
var MapboxClient = require('mapbox');
var client = new MapboxClient('');
client.listStyles(function (err, styles) {
console.log(styles);
});
2.在你的 app.js 中包含 examplefile.js
<script src="examplefile.js" type="text/javascript"></script>
3.然后从你的 app.js 文件中通过 onclick() 事件调用 a()
<input type="button" onclick="javascript: a();" value="button"/>
这就是我的理解,你正在尝试做。
关于javascript - Node.js:通过前端执行服务器脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47997249/