Skip to content
Permalink
Newer
Older
100644 44 lines (38 sloc) 1.55 KB
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
*/
6
// include ace search path and modules
Nov 9, 2011
7
require("amd-loader");
8
9
var http = require("http");
10
var fs = require("fs");
11
var resolve = require("path").resolve;
12
13
// load the highlighter and the desired mode and theme
Nov 9, 2011
14
var highlighter = require("../../lib/ace/ext/static_highlight");
15
var JavaScriptMode = require("../../lib/ace/mode/javascript").Mode;
16
var theme = require("../../lib/ace/theme/twilight");
17
18
var port = process.env.PORT || 2222;
19
20
http.createServer(function(req, res) {
21
var url = req.url;
22
var path = /[^#?\x00]*/.exec(url)[0];
23
var root = resolve(__dirname + "/../../").replace(/\\/g, "/");
24
path = resolve(root + "/" + path).replace(/\\/g, "/");
25
if (path.indexOf(root + "/") != 0)
26
path = __filename;
27
res.writeHead(200, {"Content-Type": "text/html; charset=utf-8"});
28
fs.readFile(path, "utf8", function(err, data) {
29
if (err) data = err.message;
30
var t = Date.now();
31
var highlighted = highlighter.render(data, new JavaScriptMode(), theme);
32
console.log("Rendered " + (data.length/1000) + "kb in " + (Date.now() - t) + "ms");
33
res.end(
34
'<html><body>\n' +
35
'<style type="text/css" media="screen">\n' +
36
highlighted.css +
37
'</style>\n' +
39
'</body></html>'
40
);
41
});
42
}).listen(port);
43
Jul 19, 2022
44
console.log("Listening on port " + port);