NodeJS Render parameter to local JS - javascript

I am looking for an information.
How to pass a variable from NodeJS res.render('index', { title: "Hey"}) to local JS.
I use express (with helmet) and PugJs. I can include my var in the template, but I want to store it on the JS file who's part of the template. (No inline script, vanilla JS)
Currently, I use a hidden Input in the template to store the data, the JS script get the input and do the work.
How I can pass the variable thought the template to my JS file?
Sorry, I 'm French :(
Thank you for your time!

Related

How to pass data from nodejs to inside html script tags

I'm using expressjs as a server on 8000 port. I want to send a string value from expressjs file to html script tags and use this string value in script tags.
name variable is coming as a empty string now.
How can i console.log name variable's value?
static-pages-server.js:
app.get('/index', function(req, res) {
var name = "hello";
res.render(__dirname + "/static-pages/journey-analize-report/index.html", {name:name});
});
index.html:
<script type="text/javascript">
console.log(name);
</script>
Edit : I used ejs and now problem is how should i describe name attribute in script tags? Below code is giving syntax error.
<script type="text/javascript">
console.log(<%=name%>);
</script>
Express.js itself is a backend server. If you would like to have dynamic HTML files you need to use templates engines.
Please follow this document -> https://expressjs.com/en/guide/using-template-engines.html
Eventually, you will realize you will need a frontend framework to write your code faster with good quality. So also recommend you to take a look at some of the frameworks like React, Vue.js. If you need Single Page Applications you only use express.js to provide data not to render HTML. If you need Server-side rendering it is good to investigate Next.js, Nuxt.js.
You cant directly inject variables into a html file in nodejs . That is why you have templating engines in express. Check out ejs.
It would allow you to pass data directly from your routes into the page you are rendering.
Local variables sent to a view via the locals parameter using the res.render() method aren't directly accessible. Instead you need to refer to those using it's variable name wrapped inside double curly brackets. So if you want to use it inside a JavaScript function, you need declare a local variable and give it the content of your name local.
Simply modify your index.html like this:
<script type="text/javascript">
let name = "{{name}}";
console.log(name);
</script>
here is the answer How do I use HTML as the view engine in Express?
what you will need is a view engine package in your app. there are many view engines currently available.
then set that view engine in express app. Trigger the view render via a call from your route’s response like you have done above.
Then in your view render the html output using the view variables and if these variables are outputted into html you can use them in your in browser JavaScript. You can also call a service in your html sending the dynamic data as well.
check out esj or pug (ps pug is my personal favourite )

Passing Django data to Require.js module

I'm working on a Django webapp and I need to pass data to the Javascript code (I use Require.js) when the page loads. Either I pass the data itself or information that I can later use for querying via Ajax.
I know I could create a global variable in my base.html template like this:
<script language="javascript">
var djangoData = "{{ data }}";
</script>
But it feels wrong to include it in the HTML and it won't be minified/uglified. I don't like to expose the inner logic so explicitly.
Ideally, I would have liked to create a Require.js module that will be populated with the relevant data from Django and that I can optimize (using r.js) and minify.
I would appreciate any suggestion.

how do i access server data in separate js file

aspx file
string firsrName="jafer";
myscript.js
GetMyName();
function GetMyName() {
alert('<%=firstName%>');
}
I am not getting my value
The line alert('<%=firstName%>'); use the Web Form Page syntax. It is actually not possible to get the value like this because this syntax cannot be used in external JS files.
The simpliest (but not cleanest) method is to write the JS method into the layout file or another aspx file.
Read How to get asp.net client id at external javascript file
You could make a global variable in your aspx page and access it in your js using window.objectName

Pass Node env to layout.jade in Express without explicitly defining route

So, I get passing parameters to Jade files- piece of pie:
app.use('/myroute', function (req, res) {
res.render('myview', {somevar: 'Testing!'});
});
However- I have my layout.jade file that is implicitly read and rendered by Express, with no actual definition of route-view binding, and I'm trying to work out how to pass a variable into that template.
Any ideas? Cheers!
Ps- this is so that I can pass node's development/production variable to some front-end javascript that's being added on every page.
Ah, I worked out the answer. Set it as a local variable in express, as mentioned here: Express.js View "globals"
So, in app.js I included app.locals.env = app.settings.env; and then in my layout.jade I added my javascript and simply used #{env} inside the script to get the environment value.
You can also use #{settings.env} in the jade template, which get the value from app.locals.settings.env

Node.js: Pass variables to a file

I am sending a .XML file using res.sendfile 'xml/foo.xml'. How can I pass variables to the foo.xml file and change the .XML content accordingly?
Thanks
Instead of using res.sendfile 'xml/foo.xml', you'll have to use a template file and pass your variables into a library that will parse the template and give you the XML for each request. While there are many templating engines for Node.js, the two that Express supports by default are EJS and Jade. Jade is Haml-like, which is cool, but if you're working with existing XML, then you should probably stick to EJS. EJS lets you just take ordinary HTML or XML and embed JavaScript in it.
So, you'd rename xml/foo.xml to views/foo.ejs and, instead of res.sendfile, you'd write something like
res.contentType 'text/xml'
res.render 'foo.ejs', obj
where obj contains all of the variables that you want to make available to the template.
Check the Express guide on "View Rendering" for more information.

Categories

Resources