Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

319
Views
How do I get data from server route to javascript? Nodejs Express pug

I am pretty new at this so I hope i'm asking the right question, and approaching this the right way. I am trying to get data from the server. I have successfully passed it through a route to a pug page, but the javascript on that page does not have access to these objects. Here is the server code. Also posted is the client side javascript where I don't have access to the objects passed to the pug page.

router.get('/stockView', ensureAuthenticated, function(req, res, next){

  var s = req.user.stocks;
  var t = [];

  // get array of stock symbols
  for(var stock in s)
    {
      if (isNaN(parseInt(stock)))
      {    
      }
      else
      {
        s.push(req.user.stocks[stock].symbol);
      }
    }

  //pull historic data from yahoo-finance
  yahooFinance.historical({
    symbols: ['AAPL','YHOO'],
    from: '2012-01-01',
    to: '2012-02-28',
    period: 'd'  // 'd' (daily), 'w' (weekly), 'm' (monthly), 'v' (dividends only) 
  }, function (err, quotes) {

    if(err){
      // do nothing
    }else{
      res.render('stockView', {title: 'Stock View', user: req.user, stocks: quotes, mystocks: s, tickers: t });
    }

  });

});


// *********javascript code in stockView.pug file****************
script(type='text/javascript').
      $(function(){
        var tickers = #{title};
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It doesn't work like that, Pug allows you to only handle the view and hence it directly renders the html view there. Pug Can only render views (HTML), It won't be able to give the javascript there access to the variables available to pug.

You can use Ajax or Socket.io instead so your javascript code on the client side is connected with the javascript code you write on the server-side.

That's Why we've technologies like socket.io and ajax, they allow the client side to be in touch with the backend and with Node I prefer to use Socket.io.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!