gpt4 book ai didi

javascript - 类型错误 : object is not a function+nodeJS

转载 作者:行者123 更新时间:2023-11-30 12:30:08 25 4
gpt4 key购买 nike

我正在按照视频学习在线类(class)中的 node js,但我在终端中收到此错误:

 TypeError: object is not a function     at cacheYEntrega (C:\Users\omex\Documents\My Web Sites\cache\server.js:24:14)     at C:\Users\omex\Documents\My Web Sites\cache\server.js:31:13     at Object.cb [as oncomplete] (fs.js:168:19)
var http = require('http');
var path = require('path');
var fs = require('fs');

var mymeTypes = {
'.js': 'text/javascript',
'.html': 'text/html',
'.css': 'text/css'
};

var cache = {}; //objeto cache que se utilizara para almacenar archivos en memoria
function cacheYEntrega(f,cb){
if(!cache[f]){
fs.readFile(f, function (err, data) {
if (!err) {
cache[f] = { content: data };
}
cb(err, data);

});
return;
}
console.log('cargando ' + f + ' de cache');
cb(null, cache(f).content);
}

http.createServer(function (request, response) {
var buscar = path.basename(decodeURI(request.url)) || 'index.html', f = 'content/' + buscar;
fs.exists(f, function (exists) {
if (exists) {
cacheYEntrega(f, function (err, data) {
if (err) { response.writeHead(505); reponse.end('Error en el servidor'); return; }
var headers = { 'Content-type': mymeTypes[path.extname(buscar)] };
response.writeHead(200, headers);
response.end(data);
});
return;
}
response.writeHead(404);
response.end('pagina no encontrada');
});




}).listen(process.env.PORT || 8080);

最佳答案

错误的原因是cache(f)需要是cache[f],因为它是一个普通的对象,而不是一个函数。

关于javascript - 类型错误 : object is not a function+nodeJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28005752/

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