-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic.js
41 lines (39 loc) · 1005 Bytes
/
static.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
var fs = require('fs');
var http = require('http');
var url = require('url');
var ROOT_DIR = "html/";
http.createServer(function (req, res) {
var urlObj = url.parse(req.url, true, false);
//install route for getcity
if(urlObj.pathname.indexOf('getcity') != -1)
{
console.log('in GetCity');
fs.readFile('cities.dat.txt', function (err,data){
if(err) throw err;
var cities = data.toString().split('\n');
var myRe = new RegExp('^' + urlObj.query['q']);
var jsonresult = [];
for(i = 0; i < cities.length; i++)
{
var result = cities[i].search(myRe);
if(result != -1){
//console.log(cities[i]);
jsonresult.push({city:cities[i]});
}
}
console.log(JSON.stringify(jsonresult));
res.writeHead(200);
res.end(JSON.stringify(jsonresult));
});
}else{
fs.readFile(ROOT_DIR + urlObj.pathname, function (err,data) {
if (err) {
res.writeHead(404);
res.end(JSON.stringify(err));
return;
}
res.writeHead(200);
res.end(data);
});
}
}).listen(80);