简体   繁体   English

将值从 node.js 传递给 javascript

[英]pass value from node.js to javascript

I'm trying to work with Google's example code: PhotoFrame .我正在尝试使用 Google 的示例代码: PhotoFrame It is built with node.js, expressjs, jQuery, etc. I need to pass a variable -- let's call it myString -- from app.js to one of the .js files -- let's call it myScript.js.它是用 node.js、expressjs、jQuery 等构建的。我需要传递一个变量——我们称之为 myString——从 app.js 到 .js 文件之一——我们称之为 myScript.js。 I've tried at least a dozen approaches that I found at StackOverflow and elsewhere.我至少尝试了在 StackOverflow 和其他地方找到的十几种方法。 None has worked.没有一个奏效。

Some folks have said "just use a global, even though they're 'BAD'. In app.js you:有些人说“只要使用全局变量,即使它们是‘坏的’。在 app.js 中,你:

global.myStringGlobal = myString

and in myScript you:在 myScript 中,您:

var myStringLocal = myStringGlobal;

Doesn't work: myStringGlobal is undefined.不起作用:myStringGlobal 未定义。

Some folks say: code a little object in app.js and module.export it.有人说:在 app.js 和 module.export 中编写一个小对象。 Them require it in myScript.他们在 myScript 中需要它。 Doesn't work: require is undefined in myScript.不起作用:需要在 myScript 中未定义。 In fact, since myScript and myOtherScripts run in the browser, require is simply un-available.事实上,由于 myScript 和 myOtherScripts 在浏览器中运行,require 根本不可用。

Some folks say code this in app.js:有些人说在 app.js 中编码:

res.render(page, {myString: myString});

Others say "No, that sends myString to the html renderer; not to myString. They're correct.其他人说“不,将 myString 发送到 html 渲染器;而不是 myString。他们是正确的。

I won't drag this out by telling you the other ways I tried in vain.我不会通过告诉你我徒劳地尝试的其他方法来拖延这一点。

This question has been asked and answered many times in various ways, but none of the answers work -- anymore?这个问题已经以各种方式被问过和回答过很多次,但没有一个答案有效——现在吗? or who knows why?或者谁知道为什么?

So I ask again: does anyone know how to pass a variable from the node.js app.js script to some myScript.js file?所以我再问一次:有谁知道如何将 node.js app.js 脚本中的变量传递给某个 myScript.js 文件?

Here's the explanation.这是解释。

You have to know that you have been involved into different app.你必须知道你参与了不同的应用程序。 Your express app is a server side app, while another would be client side app.您的express应用程序是一个服务器端应用程序,而另一个是客户端应用程序。 So, the main question is, how to pass data from server to client?那么,主要的问题是,如何将数据从服务器传递到客户端?

If you're thinking about ajax , then you're right.如果您正在考虑ajax ,那么您是对的。 I'm not using jQuery anymore, but I think you can use something like $.get(..) .我不再使用jQuery ,但我认为你可以使用类似$.get(..)东西。

server服务器

const express = require('express');
const app = express();
const port = 8080;

// Data can be requested via GET method
app.get('/sample-api', (req, res) => {
    res.json({ text: 'Lorem Ipsum' });
});

// You can render page here...

app.listen(port, () => console.log(`Listening on port ${port}`);

client客户

$(function() {
    var msgFromServer;
    $.get( "/sample-api", function( data ) {
        msgFromServer = data.text;
        alert( "Received data from server!" );
    });
});

From Expressjs, you can pass a variable along with a page when you render it .在 Expressjs 中,您可以在渲染页面将变量与页面一起传递。 In the render call, you name the view you're trying to render and you can pass variables along with it:在渲染调用中,您命名要渲染的视图,并且可以随它传递变量:

app.render('email', { name: 'Tobi' }, function(err, html){
  // ...
});

This passes a variable named 'name' to the email view with the value 'Tobi'.这会将名为“name”的变量传递给值为“Tobi”的电子邮件视图。 You can then use the variable in the HTML然后您可以在 HTML 中使用该变量

<script>var name = "<%= name %>";</script>

See previous question见上一个问题

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM