Node.js app检查错误的目录

时间:2014-04-24 03:06:53

标签: javascript windows node.js

首先,这是在尝试运行我的server.js时出现在cmd上的错误:

C:\Users\Jacob\Desktop\setgame>node server.js

fs.js:427
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^
Error: ENOENT, no such file or directory 'C:\Users\Jacob\Desktop\setgame\C:\User
s\Jacob\Desktop\setgame\deps\JSON-js\json2.js'
    at Object.fs.openSync (fs.js:427:18)
    at Object.fs.readFileSync (fs.js:284:15)
    at Build.add (C:\Users\Jacob\Desktop\setgame\node_modules\ams\lib\build.js:1
32:34)
    at buildStaticFiles (C:\Users\Jacob\Desktop\setgame\server.js:135:6)
    at Object.<anonymous> (C:\Users\Jacob\Desktop\setgame\server.js:17:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

我很困惑。 我在Windows 8.1.1 x64上运行node.js 0.10.26。 有任何修复此错误的建议吗?

编辑:这是server.js文件:

var http = require('http')
  , fs = require('fs')
  , io = require('socket.io')
  , connect = require('connect')
  , gzip = require('gzippo')
  , nowww = require('connect-no-www')
  , ams = require('ams')
  , Game = require('./game')
  , server
  , games = {}
  , latestPublicGame
  , clientDir = __dirname + '/client'
  , publicDir = __dirname + '/public'
  , depsDir = __dirname + '/deps'
  , prod = process.env.NODE_ENV === 'production';

buildStaticFiles();

function niceifyURL(req, res, next){
  if (/^\/game\/public/.exec(req.url)) {
    res.writeHead(302, {
      'Location': '/game/#!/' + getLatestPublicGame().hash
    });
    return res.end();
  }
  if (/^\/game$/.exec(req.url)) {
    res.writeHead(301, { 'Location': '/game/' });
    return res.end();
  }
  if (/^\/game\//.exec(req.url)) {
    req.url = '/game.html';
  } else if (/^\/about/.exec(req.url)) {
    req.url = '/about.html';
  } else if (/^\/help/.exec(req.url)) {
    req.url = '/help.html';
  } else if (/^\/?$/.exec(req.url)) {
    req.url = '/index.html';
  }
  return next();
}

server = connect.createServer(
    connect.logger(':status :remote-addr :url in :response-timems')
  , nowww()
  , niceifyURL
  , gzip.staticGzip(publicDir, {
        matchType: /text|javascript/
      , maxAge: prod ? 86400000 : 0
    })
  , gzip.staticGzip(publicDir + '/perm', {
        matchType: /image|font/
      , maxAge: prod ? 604800000 : 0
    })
);

server.listen(prod ? 80 : 8000);

io = io.listen(server);
io.configure('production', function() {
  io.enable('browser client minification');  // send minified client
  io.enable('browser client etag');          // apply etag caching logic based on version number
  io.enable('browser client gzip');          // gzip the file
  io.set('log level', 1);                    // reduce logging
  io.set('transports', [                     // enable all transports (optional if you want flashsocket)
      'websocket'
    , 'flashsocket'
    , 'htmlfile'
    , 'xhr-polling'
    , 'jsonp-polling'
  ]);
});

function getUnusedHash() {
  do { var hash = randString(4); } while (hash in games);
  return hash;
}
function getGame(hash) {
  if (hash && hash in games) return games[hash];
  hash = getUnusedHash();
  return (games[hash] = new Game(io, hash));
}

function getLatestPublicGame() {
  if (!latestPublicGame ||
    latestPublicGame.started ||
    !(latestPublicGame.hash in games))
  {
    var hash = getUnusedHash();
    latestPublicGame = games[hash] = new Game(io, hash, 3);
  }
  return latestPublicGame;
}

io.sockets.on('connection', function(socket){
  var game = null;
  socket.on('init', function(message){
    console.log('connecting socket ' + socket.id);
    game = getGame(message.game);
    game.registerClient(socket, message.sess);
    (game.handleClientMessage('init', socket)).call(game, message);
    if (message.game !== game.hash) socket.emit('setHash', game.hash);
  });

  socket.on('disconnect', function() {
    if (!game) return;
    var hash = game.hash;
    game.unregisterClient(socket, function gameOver() {
      console.log('gameover called');
      delete games[hash];
    });
    game = null;
  });
});

var CHARSET = ['A','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','T','V','W','X','Y','Z'];

function randString(size) {
  var ret = "";
  while (size-- > 0) {
    ret += CHARSET[Math.floor(Math.random() * CHARSET.length)];
  }
  return ret;
}

function buildStaticFiles() {
  var options = {
    uglifyjs: prod,
    jstransport: false,
    cssabspath: false,
    cssdataimg: false,
    texttransport: false
  };
  ams.build
    .create(publicDir)
    .add(depsDir + '/JSON-js/json2.js')
    .add(clientDir + '/util.js')
    .add(depsDir + '/jquery-bbq/jquery.ba-bbq.js')
    .add(depsDir + '/jquery.transform.js/jquery.transform.light.js')
    .add(clientDir + '/client.js')
    .combine({js: 'client.js'})
    .process(options)
    .write(publicDir)
  .end();
  ams.build
    .create(publicDir)
    .add(clientDir + '/style.css')
    .add(depsDir + '/headjs/src/load.js')
    .process(options)
    .write(publicDir)
  .end()
}

1 个答案:

答案 0 :(得分:0)

你的问题是:

, publicDir = __dirname + '/public'
, depsDir = __dirname + '/deps'

__dirname是当前脚本文件的绝对目录路径。这就是为什么您在错误消息中看到双路径名称的原因。我假设当你执行ams.build.create(publicDir)时,它会设置基本目录以及.add()所有应该相对于该基本目录(publicDir)的路径。

因此,请尝试将depsDirclientDir分别改为'deps''client'