gpt4 book ai didi

Node.js 在第一次启动时给出不可预测的 ajax 响应

转载 作者:太空宇宙 更新时间:2023-11-03 23:45:32 29 4
gpt4 key购买 nike

我使用node.js作为后端,canjs作为前端库。

Node 代码:

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"}}

每次服务器启动时,控制台都会发现两个函数(findAll 和 findOne)的 ajax GET 响应返回未定义或 500 错误:

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

发生了什么事?

最佳答案

问题在于,您在 app.init 中的 fs.readfile 回调设置其值之前调用 app.get("json");(这发生在第一次调用 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);
});

关于Node.js 在第一次启动时给出不可预测的 ajax 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13354953/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com