从javascript文件获取JSON并使用NodeJS显示在路由文件中

时间:2015-03-20 03:26:51

标签: javascript json node.js express

我正在使用Nodejs Express。我目前有一个脚本,可以从Google API生成一系列对象。我需要获取JSON数据并在我的模板中使用它。如何从路径文件中调用脚本中的函数?

这是我的脚本文件: var Spreadsheet = require(' edit-google-spreadsheet');

Spreadsheet.load({
  debug: true,
  spreadsheetId: '1eWmSV4Eq-se4gZSvBfW-J-lEOLwNopEfMavZByJ-qD8',
  worksheetId: 'owrromd',

  //    1. Username and Password
  username: 'user',
  password: 'pass',

}, function sheetReady(err, spreadsheet) {
  //use speadsheet!

  spreadsheet.receive(function(err, rows, info) {
    if (err) throw err;

    var announcementArray = [];

    //console.log(rows);
    for (x in rows) {
      var eachObject = rows[x]

      var side = eachObject['1'];
      //console.log(side);

      var type = eachObject['2'];
      //console.log(type);

      var announcement = eachObject['3'];
      //console.log(announcement);

      var announcementItem = {};
      announcementItem.side = side;
      announcementItem.type = type;
      announcementItem.announcement = announcement;

      announcementArray.push(announcementItem);
    }

    announcementArray.shift();
    console.log(announcementArray);
  });
});

这是我的路线js文件:

module.exports=function(app){

    app.get('/', function(req,res){
        res.render('index', {title:"Home page", description:"The is the description"});
    });
}

2 个答案:

答案 0 :(得分:1)

更改脚本文件的内容,我们称之为loadSheet.js

var Spreadsheet = require('edit-google-spreadsheet');

function loadSheet() {
  Spreadsheet.load({
    debug: true,
    spreadsheetId: '1eWmSV4Eq-se4gZSvBfW-J-lEOLwNopEfMavZByJ-qD8',
    worksheetId: 'owrromd',

    //    1. Username and Password
    username: 'user',
    password: 'pass',

  }, function sheetReady(err, spreadsheet) {
    //use speadsheet!

    spreadsheet.receive(function(err, rows, info) {
      if (err) throw err;

      var announcementArray = [];

      //console.log(rows);
      for (x in rows) {
        var eachObject = rows[x]

        var side = eachObject['1'];
        //console.log(side);

        var type = eachObject['2'];
        //console.log(type);

        var announcement = eachObject['3'];
        //console.log(announcement);

        var announcementItem = {};
        announcementItem.side = side;
        announcementItem.type = type;
        announcementItem.announcement = announcement;

        announcementArray.push(announcementItem);
      }

      announcementArray.shift();
      console.log(announcementArray);
    });
  });
}

//Export it to module
exports.loadSheet = loadSheet;

然后在路线js:

var ls = require('./loadSheet.js'); //Load the module, get the name of the script file

app.get('/', function(req,res){
        res.render('index', {title:"Home page", description:"The is the description"});
        ls.loadSheet();
});

答案 1 :(得分:0)

因此,您可以根据创建的其他响应调整模块。但是你需要回复loadSheet。为清楚起见,我正在删除该功能的主体。

var Spreadsheet = require('edit-google-spreadsheet');

function loadSheet(theCallback) {   //take a callback here.
  Spreadsheet.load({...yourdata...}, function sheetReady(...) {

      // create your announcementArray
      // then call the callback
      theCallBack(undefined,announcementArray);
    });
  });
}

//Export it to module
exports.loadSheet = loadSheet;

然后,从你的路线,你可以这样得到它:

var ls = require('./loadsheet.js'); // assumes in same dir as routes
app.get('/', function(req,res){
        ls.loadSheet(function(err,result){
            res.render('myTemplate',result);
        });
});

我将假设您可以将result数据放入模板中。您可以查看index模板,了解它如何吸收数据。我不知道你是使用Jade还是EJS。

注意,这是一种hackish,但解决了你的功能问题。如果您需要更进一步的指导,请告诉我。