0

I am creating application using node.js express where i want to pass some data from express and use in external javascript file.

here is my app.js

const express=require('express');

const path=require('path');

const app=express();

const bodyParser=require('body-parser');

//configure app
 app.set('view engine','ejs');
 app.set('views',path.join(__dirname,'views'));

//use middleware
 app.use(express.static(path.join(__dirname, 'bower_components')));
 app.use(express.static(__dirname + '/public'));
 app.use(bodyParser.json()); 
 app.use(bodyParser.urlencoded({ extended: true }));

//define routes


var arrayData = [
    { id: 1, desc: "Nitesh" },
   {id:2,desc:"Raviaknt"}
]




app.get('/',function(req,res){

      res.render('index', arrayData);

});




app.listen(800,function(){

    console.log('hi');

})

here is my index.ejs file

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link rel="stylesheet" href="bootstrap/dist/css/bootstrap.css"/>
</head>
<body>

       <script src="demo.js"></script>

</body>
</html>

here is my last demo.js file

here how do i use arrayData which am passing in express

5
  • You can pass your data by inserting it in some hidden element in your template file and then picking and parsing that value on front end using javascript/jquery. Commented Jan 11, 2017 at 13:23
  • Place <script type='text/javascript'>var arrData =<%-JSON.stringify(arrayData)%></script> inside your index.ejs and see if it works. Commented Jan 11, 2017 at 13:26
  • @DavidR arrayData is not defined Commented Jan 11, 2017 at 13:27
  • Sorry, I forgot to mention that, in your app.js you need to change your app.get('/'...) as app.get('/',function(req,res){res.render('index',{arrayData: arrayData});}); Commented Jan 11, 2017 at 13:30
  • Yes after that it is working only in index.ejs but any idea how to use in external js file which is linked in index.ejs Commented Jan 11, 2017 at 14:09

1 Answer 1

3

You have to put it somewhere that the JavaScript can access it. (The JavaScript being external isn't particularly relevant).

At the moment, you are passing it to the template renderer and then ignoring it.

Generally, you should be passing an object to the template renderer, not an array, so you would want something more like this:

res.render('index', { myArray: arrayData });

How you then access it depends on exactly which template engine you are using.

There are several ways you could inject it into the page.

  • You could create visible data, possibly by generating a table element with associated rows and data cells.
  • You could store it in <meta> elements, or in data-* attributes.
  • You could generate a <script> element and make it a global variable.

Note that with the latter two approaches you will either be better off serialising the data to a JSON string or doing so it a pretty hard requirement for the technique to work.

You could also forget about injecting it into the page and make it available through seperate URL that just outputs it as JSON. You could then have the client side JavaScript use XMLHttpRequest or fetch to request it.

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

7 Comments

after your suggestion its working fine when putting like this in index.ejs <script type='text/javascript'> var arrayData =<%-JSON.stringify(myArray)%> alert("somevalue"+arrayData); but it does not work when i put this code in demo.js file any idea about ?
@Deepak — demo.js is being rendered by app.use(express.static(__dirname + '/public')); and not by res.render('index', { myArray: arrayData });. It isn't template file and you aren't passing any data to it from the express route.
I am new in this can you please help me what code i have to write so that i can access that data in my external javascript file
You'd need to write an Express route in the style of app.get('/',function(req,res){ to do it. It's probably not a good idea though. Keeping your data separate from your logic makes things easier to manage.
Then according to you what is the best way to get access data in external Js file from express.if there is any blog then share so that i can learn and can do it.
|

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.