6

this is my code for a simple node.js application:

var http = require('http'); var fs = require('fs'); var path = require('path'); var url = require('url'); var port = process.env.port || 1337; http.createServer(function onRequest(req, res) { var urlParts = url.parse(req.url); var doc = '/docs' + urlParts.pathname; path.exists(doc, function fileExists(exists) { if (exists) { res.writeHead(200, { 'Content-Type': 'text/plain' }); fs.createReadStream(doc).pipe(res); } else { res.writeHead(404); res.end('Not Fouind\n'); } }); }).listen(port); 

When I try to run it I get an error that reads:

path.exists(doc, function fileExists(exists) { ^ TypeError:Undefined is not a function 

This is copied off of a tutorial so I'm not sure what's going on. (PS: I'm using Visual Studio)

1
  • @DavidSherret — But named function expressions are easier to debug when you get a stacktrace. Commented Jun 17, 2015 at 20:42

2 Answers 2

11

I think path.exists is deprecated. I've been using fs.exists. Try that.

Sign up to request clarification or add additional context in comments.

1 Comment

@dismal1290 as a courteous gesture to the answerer, can you please mark it as accepted answer if it resolved your problem.
4
fs.exists(doc, function fileExists(exists) { if (exists) { res.writeHead(200, { 'Content-Type': 'text/plain' }); fs.createReadStream(doc).pipe(res); } else { res.writeHead(404); res.end('Not Fouind\n'); } }); 

path.exists is not a function, the function exists in the fs module under fs.exists.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.