1

I'm trying to work with Google's example code: 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. I've tried at least a dozen approaches that I found at StackOverflow and elsewhere. None has worked.

Some folks have said "just use a global, even though they're 'BAD'. In app.js you:

global.myStringGlobal = myString

and in myScript you:

var myStringLocal = myStringGlobal;

Doesn't work: myStringGlobal is undefined.

Some folks say: code a little object in app.js and module.export it. Them require it in myScript. Doesn't work: require is undefined in myScript. In fact, since myScript and myOtherScripts run in the browser, require is simply un-available.

Some folks say code this in app.js:

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

Others say "No, that sends myString to the html renderer; not to myString. They're correct.

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?

2
  • As an alternative, maybe you can pass it via ajax. Commented Jul 21, 2018 at 23:08
  • This question title is like "pass value from clang to c++". Or maybe "pass value from Rails to Ruby". Commented Jul 21, 2018 at 23:17

2 Answers 2

1

From Expressjs, you can pass a variable along with a page when you render it. 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'. You can then use the variable in the HTML

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

See previous question

Sign up to request clarification or add additional context in comments.

2 Comments

This approach hasn't worked for me. The app.js code: 'res.render(page, {authTok: authTok});'. The album.ejs code:'<script>authorizeToken = "<%= name %>";</script>'. The album.js code: 'var tok = authorizeToken' yields undefined.
The variable being set in the render is authTok. You need to use that variable name in the <%= %>
1

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. So, the main question is, how to pass data from server to client?

If you're thinking about ajax, then you're right. I'm not using jQuery anymore, but I think you can use something like $.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!" );
    });
});

1 Comment

Yes. You are indeed passing from server-side to client-side. Can you be more specific about how to code the ajax?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.