-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
81 lines (67 loc) · 2.49 KB
/
server.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
First make this file: package.json
{
"name": "Node-Mock-Server",
"description": "A mock http server for testing your client side code via the node.js express serving up static content and registering your custom/mocked routes.",
"version": "0.0.2",
"private": true,
"dependencies": {
"express": "3.1.1",
"walkdir": "0.0.5"
"underscore": "1.4.4"
}
}
Then install the app using
npm install
npm install underscore
npm install walkdir
*/
var express = require('express'),
app = express(),
walk = require('walkdir'),
__ = require("underscore"),
status = "starting",
pathEndingToMatch = "/mockRoutes",
paths = walk.sync('.');
app.use(express.cookieParser());
app.use(express.bodyParser());
app.basedir = __dirname;
// Lets hold on to all of the instances of our route code.
module.exports.customRoutes = [];
app.status = "configuring custom routes";
console.log("status: " + app.status);
var currentIncludePath = "";
__.each(paths, function (path) {
var pathEndingPart = (path.length >= pathEndingToMatch.length ? path.substring(path.length - pathEndingToMatch.length) : "-./");
var currentPathStartingPart = (currentIncludePath.length > 0 ? path.substring(0, currentIncludePath.length) : "-./");
if (pathEndingPart === pathEndingToMatch) {
currentIncludePath = path;
}
else if (currentPathStartingPart === currentIncludePath) {
var newRoutes = require(path)();
exports.customRoutes.push(newRoutes);
newRoutes.registerRoutes(app);
}
});
app.status = "configuring default routes";
console.log("status: " + app.status);
// Default routes that are always here
app.get('/status', function(req, res){
var body = "<html><head><title>Node Mock Server Status</title></head><body><h1>Node Mock Server Status: " + app.status + "</h1><h3>Loaded Routes:</h3><ul>";
__.each(module.exports.customRoutes, function(route) {
body += "<li>" + route.getRouteDisplayInformation() + "</li>";
});
body += "</ul></body></html>";
res.setHeader('Content-Type', 'text/html');
res.setHeader('Content-Length', body.length);
res.end(body);
});
// Serve our static content from the public folder.
app.status = "configuring static content";
console.log("status: " + app.status);
app.use(express.static(__dirname + '/public'));
// We can use directories outside of our web root.
// app.use(express.static('../publicStuff'));
app.listen(3000);
console.log('Node Mock Server now listening on port 3000');
app.status = "running";