This repository was archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode-modules-handler.js
More file actions
59 lines (54 loc) · 1.65 KB
/
node-modules-handler.js
File metadata and controls
59 lines (54 loc) · 1.65 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
/**
* Created by kbarbounakis on 2/9/16.
*/
/**
* @private
*/
var StaticHandler = require("./static-handler").StaticHandler,
util = require("util"),
fs = require("fs"),
url = require("url"),
path = require("path");
function NodeModulesHandler() {
}
util.inherits(NodeModulesHandler, StaticHandler);
NodeModulesHandler.prototype.mapRequest = function(context, callback) {
callback = callback || function() {};
try {
//get file path
var uri = url.parse(context.request.url);
if (!/^\/node_modules\//i.test(uri.pathname)) {
return callback();
}
var p = path.join(process.cwd(), uri.pathname);
fs.stat(p, function(err, stats) {
if (err) {
//requested file does not exists
if (err.code === "ENOENT") { return callback(); }
return callback(err);
}
else {
//if file exists
if (stats && stats.isFile()) {
//set request current handler
context.request.currentHandler = new NodeModulesHandler();
//set current execution path
context.request.currentExecutionPath = p;
//set file stats
context.request.currentExecutionFileStats = stats;
}
callback(null);
}
});
} catch (e) {
callback(e);
}
};
if (typeof exports !== 'undefined') {
module.exports = {
NodeModulesHandler:NodeModulesHandler,
createInstance : function() {
return new NodeModulesHandler();
}
};
}