使用require - Node js在客户端快速访问函数 [英] Accessing the functions in express on client side using the require -- Node js

查看:147
本文介绍了使用require - Node js在客户端快速访问函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个访问名为



test.js的文件中定义的配置变量 -

  var aws = require('aws-sdk'); 
exports.connect = function(){
return aws;
}

现在我需要在浏览器上发生OnClick事件时访问它。



clientScript.js

  var aws = require('../ scripts / test.js')。connect(); 

函数getValue(){
aws.describe({},function(){...})
}

如何访问此aws变量?

解决方案

希望我不会太离谱,你在这里试图做的事情。我的理解(在这个和你的上一个问题之间拼凑在一起)是你希望浏览器中的某些东西在点击后会从外部API中获取一些状态信息,然后显示在客户端中。



我会(基于上述假设)将您希望的功能定义为由Express服务器的HTTP请求触发的功能,Express服务器可以执行您的功能并将您希望从其流程返回给客户端的任何内容发送给客户端。 p>

在您的服务器定义中(假设您的Express变量为 app

app.get('/ request',someFunction);



someFunction 定义你想要做什么,它与请求和响应有什么关系,以及什么要发送给客户端。 nction将请求和响应作为参数,但您不一定需要使用它们:

  someFunction(req,res) {
//使用aws或其他任何东西做任何事情
res.send(foo); //其中foo是任何JSON或文本或其他任何我希望客户必须操作
}

在客户端上,您将有一个绑定到onclick的函数,它会向 / request 端点发出请求。这个函数可以使用AJAX或者完全渲染另一个页面。



这种组织还会将显示和行为留给客户端,而服务器则处理检索和操作数据。这种布局还解决了客户端对require()语句的任何疑虑(尽管可能有诸如 Browserify ,没有必要,可能会使代码更加混乱)。


I have a to access the config variables defined in the file called

test.js which has --

 var aws = require('aws-sdk');
 exports.connect = function(){
    return aws;
 }

Now I need to access it when the OnClick event occurs on the browser. I have this script but the require module does not work.

clientScript.js

var aws = require('../scripts/test.js').connect();

function getValue() {
    aws.describe({},function(){...})
}

How can I access this aws variable?

解决方案

Hopefully I'm not too far off the mark with what you're trying to do here. My understanding (cobbled together between this and your previous question is that you would like something in the browser that upon click will retrieve some status information from an external API, which will then be displayed in the client.

What I would recommend doing (based on the above assumption) is defining your desired function as something to be triggered by an HTTP request to the Express server, which can perform your function and send whatever you'd like from its process back to the client.

In your server define (assuming your Express variable is app)

app.get('/request', someFunction);

In someFunction define what it is you'd like to do, how it relates to the request and response, and what to send back to the client. Express will expect the function to take request and response as arguments, but you don't necessarily need to use them:

someFunction(req,res) {
  //do whatever I'd like with aws or anything else
  res.send(foo); //where foo is whatever JSON or text or anything else I'd like the client to have to manipulate
}

On the client, you would have a function bound to onclick that would make the request to that /request endpoint. This function could use AJAX or simply render another page entirely.

This sort of organization also leaves display and behavior to the client, while the server deals with retrieving and manipulating data. This layout also resolves any concerns about require() statements on the clientside (which, while possible with things like Browserify, are not necessary and may make the code more confusing).

这篇关于使用require - Node js在客户端快速访问函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆