-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
51 lines (45 loc) · 1.63 KB
/
index.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
var resolve = require("resolve")
var path = require("path")
var url = require("url")
var CompiledFiles = require("compiled-files")
var bundle = require("./bundle")
// /js/:appName
module.exports = CompiledFiles({
// this is the function to compile the resource.
// the location is the value returned by findResource
// you should pass a String to the callback
compile: function (location, opts, callback) {
resolve(location, function (err, fileUri) {
if (err) {
return callback(err)
}
bundle(fileUri, opts, callback)
})
},
// This is the logic of how to display errors to the user
sendError: function sendError(req, res, err) {
return res.end("(" + function (err) {
throw new Error(err)
} + "(" + JSON.stringify(err.message) + "))")
},
// CompiledFiles will set this content type on the response
contentType: "application/javascript",
/*
findResource takes a Request, the opts passed to the ServeFile
function and a callback.
It's responsibility is to return a location on disk that the
Request is asking for.
*/
findResource: function findResource(req, res, opts) {
var pathname = url.parse(req.url).pathname
var location
if (opts.base) {
var rest = pathname.substr(opts.base.length).split("/")
location = path.join.apply(null, rest.filter(Boolean))
} else {
var parts = pathname.split("/")
location = parts[parts.length - 1]
}
return path.join(opts.root, location)
}
})