1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var StaticHandler = require("./static").StaticHandler;
|
10 | var LangUtils = require('@themost/common/utils').LangUtils;
|
11 | var fs = require("fs");
|
12 | var url = require("url");
|
13 | var path = require("path");
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function NodeModulesHandler() {
|
21 | NodeModulesHandler.super_.bind(this)();
|
22 | }
|
23 |
|
24 | LangUtils.inherits(NodeModulesHandler, StaticHandler);
|
25 |
|
26 | NodeModulesHandler.prototype.mapRequest = function(context, callback) {
|
27 | callback = callback || function() {};
|
28 | try {
|
29 |
|
30 | var uri = url.parse(context.request.url);
|
31 | if (!/^\/node_modules\//i.test(uri.pathname)) {
|
32 | return callback();
|
33 | }
|
34 | var p = path.join(process.cwd(), uri.pathname);
|
35 | fs.stat(p, function(err, stats) {
|
36 | if (err) {
|
37 |
|
38 | if (err.code === "ENOENT") { return callback(); }
|
39 | return callback(err);
|
40 | }
|
41 | else {
|
42 |
|
43 | if (stats && stats.isFile()) {
|
44 |
|
45 | context.request.currentHandler = new NodeModulesHandler();
|
46 |
|
47 | context.request.currentExecutionPath = p;
|
48 |
|
49 | context.request.currentExecutionFileStats = stats;
|
50 | }
|
51 | callback(null);
|
52 | }
|
53 | });
|
54 | } catch (e) {
|
55 | callback(e);
|
56 | }
|
57 | };
|
58 |
|
59 | if (typeof exports !== 'undefined') {
|
60 | module.exports = {
|
61 | NodeModulesHandler:NodeModulesHandler,
|
62 | createInstance : function() {
|
63 | return new NodeModulesHandler();
|
64 | }
|
65 | };
|
66 | } |
\ | No newline at end of file |