forked from pshinghal/jsaSound
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathold_jade_server.js
More file actions
133 lines (104 loc) · 4.33 KB
/
old_jade_server.js
File metadata and controls
133 lines (104 loc) · 4.33 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var express = require("express")
, app = express()
, server = require('http').createServer(app)
, WebSocketServer = require('ws').Server
, wss = new WebSocketServer({server: server})
, fs = require('fs');
var k_portnum = 8082;
console.log("hey myserver is starting with command line arguments:");
process.argv.forEach(function (val, index, array) {
console.log(index + ': ' + val);
});
if (process.argv.length < 3){
console.log("usage: node myserver portnum");
process.exit(1);
}
k_portnum=process.argv[2];
//****************************************************************************
var m_useRoot="/www";
// For serving individual sounds with query strings -----------
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
//-------------------------------------------------------------
// Way cool: Allow access to sounds that aren't specifically required by require.js in apps on other domains
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.use(express.static(__dirname + m_useRoot));
app.use(express.static("/Demo", __dirname ));
server.listen(process.argv[2] || k_portnum);
console.log("Connected and listening on port " + k_portnum);
wss.on('connection', function (ws) {
ws.id = id++;
console.log("got a connection, assigning ID = " + ws.id);
ws.on('close', function() {
console.log(ws.id + " is gone..." );
});
});
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Match a request for data from the client and return requested json data
app.get(["/soundList", "/soundList/ModelDescriptors"],function(req, res){
var jsonObj;
var jsonList=[];
console.log("fetching from ModelDescriptors");
// get list of file names
getFileList("./www/ModelDescriptors", function (z, flist){ // TODO: The 1st arg should obviously be passed in as data from the client!
for(i=0;i<flist.length;i++){
// clean list so paths are relative to client directory
flist[i]=flist[i].replace(m_useRoot, "");
//console.log("results are" + flist);
jsonObj= JSON.parse(fs.readFileSync("./" + m_useRoot + "/" + flist[i], 'utf8'));
console.log("jsonObj.file name is " + jsonObj.fileName);
// could test for existence here before sending the info back to the client...
jsonList.push(jsonObj);
}
res.send({"jsonItems": jsonList});
});
});
// Match a request for data from the client and return requested json data
app.get(["/soundList/TestModelDescriptors"],function(req, res){
var jsonObj;
var jsonList=[];
console.log("fetching from TestModelDescriptors");
// get list of file names
getFileList("./www/TestModelDescriptors", function (z, flist){ // TODO: The 1st arg should obviously be passed in as data from the client!
for(i=0;i<flist.length;i++){
// clean list so paths are relative to client directory
flist[i]=flist[i].replace(m_useRoot, "");
//console.log("results are" + flist);
jsonObj= JSON.parse(fs.readFileSync("./" + m_useRoot + "/" + flist[i], 'utf8'));
console.log("jsonObj.file name is " + jsonObj.fileName);
// could test for existence here before sending the info back to the client...
jsonList.push(jsonObj);
}
res.send({"jsonItems": jsonList});
});
})
// Generic function that recursivley searches a directory for files
// return: list of full pathnames
var getFileList = function(dir, done) {
console.log("walking the walk");
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = dir + '/' + file;
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
getFileList(file, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
results.push(file);
if (!--pending) done(null, results);
}
});
});
});
};
exports.server = server;