Reputation: 23
I am learning node js using an online course, following the video but I got this error in the terminal:
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);
Upvotes: 1
Views: 631
Reputation: 106736
The reason for the error is that cache(f)
needs to be cache[f]
, because it's a plain object, not a function.
Upvotes: 3