14

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

3 Answers 3

40

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"));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Cromax. @ManishS, you may mark this as the correct answer
1

use __dirname instead of _dirname (Missing one underscore in your code)

Comments

0

Use __dirname instead of _dirname (You are missing one underscore in your code).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.