2017-02-15 57 views
-1

我正在製作一個在線HTML5遊戲,這些是我的代碼。HTML5 Express Node.js - _dirname未定義

app.js

var express = require('express'); 
var app = express() 
var serv = require('http').Server(app); 

app.get('/',function(req, res) { 
    res.sendFile(_dirname + '/client/index.html'); 
}); 
app.use('/client', express.static(_dirname + '/client')); 

serv.listen(2000); 

的index.html

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Game</title> 
</head> 
<body> 
Hello world 
</body> 
</html> 

當我在 '節點app.js' 控制檯(在正確的目錄,我已經選中)鍵入它給我的錯誤

ReferenceError: _dirname is not defined 
    at Object.<anonymous> (C:\Users\Amitai\Documents\AmitaiHP\app.js:8:35) 
    at Module._compile (module.js:570:32) 
    at Object.Module._extensions..js (module.js:579:10) 
    at Module.load (module.js:487:32) 
    at tryModuleLoad (module.js:446:12) 
    at Function.Module._load (module.js:438:3) 
    at Module.runMain (module.js:604:10) 
    at run (bootstrap_node.js:394:7) 
    at startup (bootstrap_node.js:149:9) 
    at bootstrap_node.js:509:3 

請幫助,有人,請不要標記這個問題,因爲你不能回答它(這發生了很多m e)

回答

0

想想你要找的是__dirname。這是兩個下劃線。

Node.js documentation

+0

我是個白癡。謝謝。 –

0

您需要兩個下劃線:__dirname,而不是_dirname

+0

謝謝,這是一個小的,愚蠢的錯誤。 –