Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  170] [ 6]  / answers: 1 / hits: 28818  / 11 Years ago, sun, september 1, 2013, 12:00:00

My directory look like this:




/config.json , /server.js , /staticFiles/welcome.html




Running server.js gives error:




app.use(express.static(_dirname + /staticFiles));
^ ReferenceError: _dirname is not defined




My 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-------------------

More From » node.js

 Answers
20

You are using one underscore while this variable actually has two underscores at the beginning:
http://nodejs.org/docs/latest/api/globals.html#globals_dirname



So use



app.use(express.static(__dirname + /staticFiles));


instead of



app.use(express.static(_dirname + /staticFiles));

[#75972] Friday, August 30, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darennevina

Total Points: 422
Total Questions: 128
Total Answers: 105

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
;