-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
41 lines (33 loc) · 1.14 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
/*
This script starts a web server and waits for incoming http requests. Requests should have the form:
http://myserver:port/VIN[?refresh]
VIN is your vehicle identification number
for example WBAXXXXXXXV123456
?refresh is an optional parameter.
If set, fresh data is download from the BMW servers. If left out, a cached copy
from stored JSON files is returned instead
*/
var http = require("http");
var url = require('url');
var fs = require('fs');
const g_serverListenPort = 8777;
var vehicle = require('./vehicle.js');
http.createServer(function (req, res)
{
var requestUrl = url.parse(req.url);
var path = requestUrl.pathname;
var search = requestUrl.search;
var vin = path.substring(1);
vehicle.requestVehicles(function(_vin, _data)
{
res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'});
res.end(JSON.stringify(_data));
}, function(_success)
{
}, function(err)
{
console.log("Error: " + err);
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('Request Error: ' + err);
});
}).listen(g_serverListenPort);