-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (71 loc) · 2.37 KB
/
app.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
//Global Variable Declarations
var appIndex = __dirname+'/public';
//Middleware
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs');
var request = require('request');
var xml2js = require('xml2js');
// Reusable middleware declarations
var parser = new xml2js.Parser(); //Per documentation, only create one parser object per file.
app.get('/', function(req, res){
var options = {
root: appIndex,
};
res.sendFile('index.html',options);
});
io.on('connection', function(socket){
//Bind User Connection Behaviors
// Get optional filters from USAJobs schema to prepopulate form
request('https://schemas.usajobs.gov/Enumerations/OccupationalSeries.xml', function (error, response, body) {
if (!error && response.statusCode == 200) {
var results;
//Convert XML Response to JSON and parse results from custom property requirements
parser.reset();
parser.parseString(body, function (err, result) {
//Grab Results for the CodeList item with the ID of OccupationSeriesFamily
data = result.CodeLists.CodeList;
for(var property in data){
//Presumes that the attrkey defaults to $
if(data[property].$.id == 'OccupationSeriesFamily'){
result = data[property];
}
}
fs.writeFile('test.json',JSON.stringify(result));
results = result;
});
//Pump Available Options into Web Interface Search Form
io.emit('job search series options',results.ValidValue);
//Debugging Purposes
// for(var key in results.ValidValue){
// console.log(results.ValidValue[key].Value,results.ValidValue[key].JobFamily);
// }
}
})
//Bind User Disconnect Behaviors
socket.on('disconnect',function(){
console.log('User socket disconnection detected.');
});
//Bind Custom Event Messaging Behaviors
socket.on('job search',function(search){
var options = {
url: 'https://data.usajobs.gov/api/jobs?series=2210',
headers: {
'Content-Type': 'application/json'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(response);
console.log(body);
}
console.log('Search initialized from external endpoint: '+search);
io.emit('job search',search);
};
request(options,callback);
});
});
http.listen(3000, function(){
console.log('Server instantiated and listening on *:3000');
});