// check if a file exists - read the api docs to learn how to check if a file exists // there is a special variable in each module __dirname it contains the directory name of this file var http = require('http'); var url = require('url'); var fs = require('fs'); http.createServer(function (req, res) { var purl=url.parse(req.url,true); if(purl.pathname=='/test') { res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'}); res.end('Test'); } else { fs.exists(__dirname+purl.pathname, function (exists) { if (exists){ res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'}); fs.readFile(__dirname+purl.pathname, function (err, data){ if (err) throw err; res.end(data); }); } else { res.writeHead(404, {'Content-Type': 'text/plain; charset=UTF-8'}); res.end('File Not Found'); } }); } }).listen(1337); console.log('Server running at http://127.0.0.1:1337/');