-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
53 lines (45 loc) · 1.19 KB
/
api.js
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
var http = require('http');
var path = require('path');
var gulpjit = require('./lib/gulpjit');
var mediatype = require('./lib/mediatype');
function _infill(options) {
if (!('gulpfile' in options)) {
options['gulpfile'] = path.resolve([__dirname, 'gulpfile.js'].join('/'));
}
return options;
}
function handler(options) {
options = _infill(options);
var builder = gulpjit.configure(options);
function handleRequest(req, res) {
builder.get(req, function(file, error) {
if (!file || !file.contents) {
error = {
code: 404,
message: 'Not Found'
};
}
if (error) {
console.log('GET', req.url, error.code);
res.writeHead(error.code);
res.end(error.message);
return;
}
var content = file.contents.toString();
var type = mediatype(file.relative);
console.log('GET', req.url, 200, ',', content.length + ' bytes');
res.writeHead(200, {
'Content-Length': content.length,
'Content-Type': type
});
res.end(content);
});
}
return {
handleRequest: handleRequest,
accepts: builder.accepts.bind(builder)
}
}
module.exports = {
handler: handler
};