-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
146 lines (107 loc) · 4.52 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
/*****************************************************
*
* File: server.js
* Description: File that runs the Batch Search
* Engine site.
*
****************************************************/
// Use express to create a server.
var express = require('express');
var app = express();
// Use constants for pre-defined data not part of the JSONs.
var constants = require('./Public/js/constants.js');
// Use a common directory for files to be used in the
// web pages, such as for images, javascript, and css.
app.use(express.static(__dirname + '/Public'));
var fs = require('fs');
var searchEngine = require('./Public/js/search.js');
// Read in the JSON files and parse them into objects to pass as data.
var data = JSON.parse(fs.readFileSync('./Public/batches.json'));
// Home Page is Song Title and Step Artist Search.
app.get('/', function(req, res) {
var inputSongTitle = ''; // song title search placeholder
var inputStepArtist = ''; // song artist search placeholder
// If there was a query from the song title form, run a song title search.
if (req.query.inputSongTitle != undefined) {
inputSongTitle = req.query.inputSongTitle;
var searchResults = searchEngine.songNameSearch(req.query.inputSongTitle, data);
}
// If there was a query from the song artist form, run a song artist search.
if (req.query.inputStepArtist != undefined) {
inputStepArtist = req.query.inputStepArtist;
var searchResults = searchEngine.stepArtistSearch(req.query.inputStepArtist, data);
}
res.render('search.jade',
{results: searchResults, inputSongTitle: inputSongTitle, inputStepArtist: inputStepArtist, data: data});
});
// Song Info Page.
app.get('/songInfo', function(req, res) {
var permission = ''; // permission value placeholder
var searchResults;
for (var keyPack in data) {
if (data.hasOwnProperty(keyPack)) {
packArray = data[keyPack];
searchResults = searchEngine.getSongById(req.query.idNum, data.length, packArray);
if (searchResults != -1)
break; // We found the file with the id
} else {
continue;
}
}
res.render('songInfo.jade',
{songTitle: searchResults.title,
artist: searchResults.artist,
stepper: searchResults.stepper,
batchName: searchResults.batch,
latest: searchResults.latest,
status: searchResults.status,
permission: permission});
});
// Displaying current batches
app.get('/batches', function(req, res) {
res.render('batches.jade');
});
// Displaying songs for one batch
app.get('/batch', function(req, res) {
var results = -1; // Song results for the specified batch.
// If there was a query from the song title form, run a song title search.
if (req.query.batchName != undefined) {
if ( searchEngine.contains(constants.batchNames, req.query.batchName) )
results = data[req.query.batchName]; // Get the array of song objects in that batch if valid
}
res.render('batch.jade',
{results: results, batchName: req.query.batchName});
});
app.get('/status', function(req, res) {
var cQueue = searchEngine.getFilesByStatus('Conditional Queue', data); // Songs in the conditional queue
var aQueue = searchEngine.getFilesByStatus('Accepted', data); // Songs in the Accepted Queue
res.render('status.jade',
{cResults: cQueue, aResults: aQueue});
});
/**************************
* AUTOCOMPLETE PROCESSING
**************************/
app.get('/autoCompleteProcessSongName', function(req, res) {
// return JSON result
//console.log("#" + req.query.inputString);
var searchResult = searchEngine.autocompleteSongNameSearch(req.query.inputString, data);
//console.log(searchResult);
//("auto complete search finished");
res.json(searchResult);
});
app.get('/autoCompleteProcessStepArtist', function(req, res) {
// return JSON result
//console.log("#" + req.query.inputString);
var searchResult = searchEngine.autocompleteStepArtistSearch(req.query.inputString, data);
//console.log(searchResult);
//("auto complete search finished");
res.json(searchResult);
});
// Error page which redirects the user back to Search.
app.get('/*', function(req, res) {
res.status(404).render('error.jade');
});
console.log('Running on port: ' + constants.port);
// Tell the server to listen to a specific port. If on localhost,
// you might need to use http://localhost:3000/ as the URL.
app.listen(constants.port, constants.ipAdress);