Advertisement
Guest User

Untitled

a guest
Mar 16th, 2010
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1.  
  2. var sys = require("sys"),
  3. http = require("http"),
  4. url = require("url"),
  5. path = require("path"),
  6. fs = require("fs");
  7.  
  8. http.createServer(function(request, response) {
  9. var uri = url.parse(request.url).pathname;
  10. var filename = path.join(/*process.cwd(),*/ uri);
  11. path.exists(filename, function(exists) {
  12. if(!exists) {
  13. response.sendHeader(404, {"Content-Type": "text/plain"});
  14. response.write("404 Not Found\n");
  15. response.close();
  16. return;
  17. }
  18.  
  19. fs.readFile(filename, "ascii", function(err, file) {
  20. if(err) {
  21. response.sendHeader(500, {"Content-Type": "text/plain"});
  22. response.write(err + "\n");
  23. response.close();
  24. return;
  25. }
  26.  
  27. response.sendHeader(200);
  28. response.write(file, "ascii");
  29. response.close();
  30. });
  31. });
  32. }).listen(8080);
  33.  
  34. sys.puts("Server running at http://localhost:8080/");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement