This repository was archived by the owner on Oct 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (49 loc) · 1.31 KB
/
index.js
File metadata and controls
59 lines (49 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var http = require('http');
var url = require('url');
var send = require('send');
var path = require('path');
var fs = require('fs');
var format = require('util').format;
require('node-jsx').install({harmony: true});
var React = require('react');
var ReactDOMServer = require('react-dom/server');
var Page = require('./page.jsx');
function renderPage(props) {
return ReactDOMServer.renderToStaticMarkup(
React.createElement(Page, props)
);
}
var app = http.createServer(function(req, res){
function error(err) {
res.statusCode = err.status || 500;
res.end(err.message);
}
function list(dirname) {
console.log('listing %s', dirname);
fs.readdir(path.join(__dirname, dirname), function(err, data) {
if (err || data.length < 1) {
res.end('no content');
return;
}
var elements = data.filter(function(d) { return d.indexOf('.') != 0 })
.map(function(d) {
return {
name: d,
path: path.join(dirname, d)
};
});
res.setHeader('Content-Type', 'text/html');
res.write('<!DOCTYPE html>');
res.end(renderPage({
url: dirname,
items: elements
}));
})
}
send(req, url.parse(req.url).pathname, {root: '.', index: false})
.on('error', error)
.on('directory', function() {
list(url.parse(req.url).pathname);
})
.pipe(res);
}).listen(process.env.PORT || 8080);