2012-11-13 26 views
1

我使用node.js作爲後端,canjs作爲前端庫。Node.js在第一次啓動時給出了不可預知的ajax響應

節點代碼:

var express = require('express'); 
var app = express(); 
var http = require("http"); 
var fs = require("fs"); 
var cons = require('consolidate') 

app.configure(function(){ 
     app.engine('html', cons.handlebars); 
     app.set('view engine', 'html'); 
     app.set('views', __dirname) 
     app.use(express.favicon()) 
     app.use(express.logger('dev')) 
     app.use(express.static(__dirname)) 
     app.use(express.bodyParser()) 
     app.use(express.methodOverride()) 
     app.use(express.cookieParser("secret")) 
     app.use(express.session({ secret: 'keyboard cat' })) 
     app.use(app.router) 
    }); 

app.init = function(){ 
    fs.readFile('./sample.json', 'utf8', function(error, data) { 
     if (error) { 
      throw(error); 
     } 
     else 
      app.set("json", data); 
    })}; 

app.get('/things', function(req, res){ 
    app.init(); 
    res.set('Content-Type', 'text/json'); 
    res.send(app.get("json")); 
}); 

app.get('/things/:id', function(req, res){ 
    app.init(); 
    res.set('Content-Type', 'text/json'); 
    res.send((JSON.parse(app.get("json")))[req.param('id')]); 
}); 

app.get('/main.html', function(req, res){ 
    app.init(function(error, data){ 
     if (error) {throw error} 
     res.render('main.html'); 
    }); 
}); 

app.listen(3000); 

Can.js代碼:

Todo = can.Model({ 
    findAll: "GET /things", 
    findOne: "GET /things/{id}", 
},{}); 

$(document).ready(function(){ 

    Todo.findAll({}, function(todos){ 
     console.log(JSON.stringify(todos)); 
    }) 

    Todo.findOne({ id: 1 }, function(todo) { 
     console.log(todo); 
    }) 

}); 

HTML:

<script type="text/javascript" charset="utf-8" src="./jquery-1.8.2.js"></script> 
<script type="text/javascript" charset="utf-8" src="./can.jquery-1.0.7.js"></script> 
<script type="text/javascript" charset="utf-8" src="./can.fixture.js"></script> 
<script type="text/javascript" charset="utf-8" src="./master.js"></script> 

JSON:

{"0":{"id":1,"name":"do the dishes"},"1":{"id":2,"name":"go to dentist"},"2":{"id":3,"name":"go swimming"},"4":{"id":5,"name":"masturbate"}} 

每次服務器啓動時,控制檯會發現這兩個函數返回未定義或500錯誤的AJAX GET響應(的findAll和findOne):

XHR finished loading: "http://localhost:3000/things". jquery-1.8.2.js:8416 
undefined master.js:12 

GET http://localhost:3000/things/1 500 (Internal Server Error) jquery-1.8.2.js:8416 
XHR finished loading: "http://localhost:3000/things/1". jquery-1.8.2.js:8416 
Uncaught TypeError: Function.prototype.apply: Arguments list has wrong type 

但如果我刷新頁面在此之後,兩個功能將正常工作,給出:

XHR finished loading: "http://localhost:3000/things". jquery-1.8.2.js:8416 
{"0":{"id":1,"name":"do the dishes"},"1":{"id":2,"name":"go to dentist"},"2":{"id":3,"name":"go swimming"},"4":{"id":5,"name":"masturbate"},"length":0,"_namespace":".observe2"} master.js:12 
XHR finished loading: "http://localhost:3000/things/1". jquery-1.8.2.js:8416 
Constructor {name: "go to dentist", _data: Object, id: 2, _namespace: ".observe3"} 
master.js:16 

什麼是GOING?

回答

2

的問題是,你打電話app.get("json");前的值已經由app.initfs.readfile回調(這在第一次調用app.init();返回後的某個時候發生)設置。

您應該更改您的代碼只調用一次app.init,並且在其工作完成之前不要監聽請求。

app.init = function(callback){ 
    fs.readFile('./sample.json', 'utf8', function(error, data) { 
     if (error) { 
      throw(error); 
     } else { 
      app.set("json", data); 
      callback(); 
     } 
    }); 
}; 

... 

app.init(function() { 
    app.listen(3000); 
}); 
+0

我認爲這比這更糟糕。看起來「main.html」請求從未被處理。它可能坐在同一個目錄下並由'express.static'服務。所以他認爲啓動sample.json的路線實際上並沒有發生(我不認爲)。之後,json每次都會被重置,但是*請求之後。因此,每個後續請求都會查看請求之前加載的JSON。 – numbers1311407

+0

這可能是我錯了,但如果主處理程序*攔截請求,它會將回調傳遞給'init',它不會接收一個,所以內部(其中呈現頁面)中的代碼不會發生。 – numbers1311407

+0

謝謝!問題解決了! – lkahtz

相關問題