TypeError:object不是函数+ nodeJS

时间:2015-01-18 00:04:55

标签: javascript node.js

我正在使用在线课程学习节点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);

1 个答案:

答案 0 :(得分:3)

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