server.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * Simple node.js server, which generates the synax highlighted version of itself
  3. * using the Ace modes and themes on the server and serving a static web page.
  4. */
  5. // include ace search path and modules
  6. require("amd-loader");
  7. var http = require("http");
  8. var fs = require("fs");
  9. var resolve = require("path").resolve;
  10. // load the highlighter and the desired mode and theme
  11. var highlighter = require("../../lib/ace/ext/static_highlight");
  12. var JavaScriptMode = require("../../lib/ace/mode/javascript").Mode;
  13. var theme = require("../../lib/ace/theme/twilight");
  14. var port = process.env.PORT || 2222;
  15. http.createServer(function(req, res) {
  16. var url = req.url;
  17. var path = /[^#?\x00]*/.exec(url)[0];
  18. var root = resolve(__dirname + "/../../").replace(/\\/g, "/");
  19. path = resolve(root + "/" + path).replace(/\\/g, "/");
  20. if (path.indexOf(root + "/") != 0)
  21. path = __filename;
  22. res.writeHead(200, {"Content-Type": "text/html; charset=utf-8"});
  23. fs.readFile(path, "utf8", function(err, data) {
  24. if (err) data = err.message;
  25. var highlighted = highlighter.render(data, new JavaScriptMode(), theme);
  26. res.end(
  27. '<html><body>\n' +
  28. '<style type="text/css" media="screen">\n' +
  29. highlighted.css +
  30. '</style>\n' +
  31. highlighted.html +
  32. '</body></html>'
  33. );
  34. });
  35. }).listen(port);
  36. console.log("Listening on port " + port);