我的目录如下所示:
/config.json,/server.js,/staticFiles/pope.html
运行server.js会产生错误:
app.use(express.static(_dirname + "/staticFiles"));^ ReferenceError:_dirname未定义
我的Server.js:
//------------Server-------------
var fs = require("fs");
var config = JSON.parse(fs.readFileSync("./config.json"));
console.log("Server UP and running..");
var host = config.host;
var port = config.port;
var express = require("express");
var app = express.createServer();
//---------Application----------------
app.use(app.router);
app.use(express.static(_dirname + "/staticFiles"));
app.get("/", function(request,response){
response.send("<h1>"/" of TrimServer</h1>");
});
app.listen(port,host);
console.log("Listening on Port -->",port);
//--------------End-------------------
发布于 2013-09-01 08:49:18
您使用的是一个下划线,而这个变量实际上在开头有两个下划线:dirname
所以用吧
app.use(express.static(__dirname + "/staticFiles"));
而不是
app.use(express.static(_dirname + "/staticFiles"));
发布于 2017-01-23 07:38:53
使用__dirname而不是_dirname (代码中缺少一个下划线)
发布于 2019-07-16 10:48:09
使用__dirname
而不是_dirname
(代码中缺少一个下划线)。
https://stackoverflow.com/questions/18556613
复制相似问题