-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic.js
More file actions
37 lines (37 loc) · 1.04 KB
/
static.js
File metadata and controls
37 lines (37 loc) · 1.04 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
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);
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"]);
console.log(myRe);
var jsonresult = [];
for (var 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(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);