简体   繁体   English

Node.js:通过前端执行服务器脚本

[英]Node.js: Execute server scripts via front-end

I'd like to execute some specific node JS files with an on-click event at my Website. 我想在我的网站on-click事件执行一些特定的node JS文件。

I'm using express to run my server and website. 我正在使用express来运行我的服务器和网站。 I think the right way to do this is using jQuery with some GET requests. 我认为正确的方法是使用jQuery和一些GET请求。 The JS files are working, if i call them simply in the console with "node examplefile.js" JS文件正在工作,如果我只是在控制台中使用"node examplefile.js"调用它们

The file looks like this: 该文件如下所示:

var MapboxClient = require('mapbox');
var client = new MapboxClient('');
client.listStyles(function (err, styles) {
    console.log(styles);
});

I want to execute this file everytime an on-click event occurs. 我希望每次发生on-click事件时都执行此文件。

Do I have to export this as a module to my app.js ? 我是否必须将此模块作为模块导出到我的app.js This is what I wanted to do, but I failed in the implementation. 这就是我想要做的,但我在实施中失败了。

Any suggestions or simple examples on how to realize this? 有关如何实现这一点的任何建议或简单示例?

It's much better to create a new module and then call it from express app 创建一个新模块然后从Express应用程序调用它会好得多

create new module (getStyles.js): 创建新模块(getStyles.js):

var MapboxClient = require('mapbox');
var client = new MapboxClient('');

module.exports = function (done) {

    client.listStyles(function (err, styles) {

        if (err) {
            return done(err);
        }

        done(null, styles);
    });

}

Use it inside express app: 在express app中使用它:

...

var getStyles = new MapboxClient('path/to/getStyles');

app.get( '/your/route/here', function (req, res, next) {

    getStyles( function (err, styles) {

        if (err) return next(err);

        res.render('view', styles)

    });

});

...

However if you want to execute a command line from express, then use exec function, here is an example: 但是如果你想从express执行命令行,那么使用exec函数,这里有一个例子:

...

const exec = require('child_process').exec;

app.get( '/on/click/buton/route', function (req, res, next) {

    exec('/usr/bin/node file/path/.js', function (err, stdout, stderr) {
        if (err) {
            return next(err);
        }
        // send result to the view
        res.render('veiw', { res:  stdout});
    });

});

...

1.Write your examplefile.js within a function 1.在函数中编写examplefile.js

function a(){
var MapboxClient = require('mapbox');
var client = new MapboxClient('');
client.listStyles(function (err, styles) {
console.log(styles);
});

2.Include examplefile.js in your app.js 2.在app.js中包含examplefile.js

<script src="examplefile.js" type="text/javascript"></script>

3.Then call a() by onclick() event from your app.js file 3.然后通过app.js文件中的onclick()事件调用()

<input type="button" onclick="javascript: a();" value="button"/>

That is what i understood, you are trying to do. 这就是我所理解的,你正在努力做到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以响应方式将后端(通过Node.js中Express服务器上的Mongoose通过MongoDB通过MongoDB)链接到前端(引导程序或基金会)? - Linking back-end (MongoDB via Mongoose, on Express server in Node.js) to front-end (Bootstrap or Foundation) in a reactive way? 如何使用node.js后端服务器与前端和mongolab数据库进行交互? - How to use a node.js back-end server to interact with the front-end and a mongolab database? Node.js 从 JS 前端获取数组 - Node.js getting array from JS front-end 从前端将数据发送回node.js服务器 - Send data back to node.js server from front-end Node.js-jQuery。 从服务器获取PDF并显示在前端 - Node.js - jQuery. Get PDF from server and display on front-end 将Node.js脚本连接到前端项目 - Connecting Node.js script to a front-end project 使用Node.js实时修改前端HTML - Modify front-end HTML live with Node.js Java / Android前端客户端和节点js后端服务器? - Java/Android front-end client and node js backend server? 这是否可以在服务器端使用带有 Node.JS 的前端 JS 框架来创建用于内容交付的 Web 服务? - Is this possible to use a front-end JS Framework on the server side with Node.JS to create a Web service for content delivery? 在前端和后端 (Node.JS) 上使用 JS 的直观方式 - Intuitive Way to Use JS on Both Front-End and Back-End (Node.JS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM