-
Notifications
You must be signed in to change notification settings - Fork 38
/
server.js
124 lines (85 loc) · 3.15 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
const bodyParser = require("body-parser");
const express = require("express");
const app = express();
var path = require('path');
var fs = require('fs');
var http = require('http');
var createFile = require('create-file');
var PythonShell = require('python-shell');
var jsonfile = require('jsonfile')
var urlencoded = bodyParser.urlencoded({ extended: false });
app.post('/location',urlencoded, (req, res) => {
var location = req.body.location;
console.log('location: '+location);
//res.send('You are now at ' + location);
//python code here *************************
var options = {
mode: 'text',
// pythonPath: 'C:/Python27',
pythonOptions: ['-u'],
// scriptPath: 'code',
args: location
};
// call to python code mlr_algo
PythonShell.run('code/mlr_algo.py',options, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log('results', results[0]);
res.send( results[0]);
});
//}); python ends here *******************************
});
//home page and rouiting of pages according to url
app.use('/', (req, res) => {
if(req.url === "/") {
fs.readFile("./public/index.html", "UTF-8", function(err,html){
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
})
}
else if( req.url === "/home"){
fs.readFile("./public/index.html", "UTF-8", function(err,html){
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
})
}else if(req.url.match("\.css$")){
var csspath = path.join(__dirname,'public',req.url);
var fileStream = fs.createReadStream(csspath, "UTf-8");
res.writeHead(200,{"content-Type": "text/css"});
fileStream.pipe(res);
}
else if(req.url.match("\.js$")){
var jspath = path.join(__dirname,'public',req.url);
var fileStream = fs.createReadStream(jspath, "UTf-8");
res.writeHead(200,{"content-Type": "scripts/js"});
fileStream.pipe(res);
}
else if(req.url.match("\.png$")){
var pngpath = path.join(__dirname,'public',req.url);
var fileStream = fs.createReadStream(pngpath);
res.writeHead(200,{"content-Type": "images/png"});
fileStream.pipe(res);
}
else if(req.url.match("\.jpg$")){
var jpgpath = path.join(__dirname,'public',req.url);
var fileStream = fs.createReadStream(jpgpath);
res.writeHead(200,{"content-Type": "images/jpg"});
fileStream.pipe(res);
}
else if(req.url === '/about'){
res.writeHead(200,{'content-Type': 'text/html'});
fs.createReadStream(__dirname + '/public/aboutus.html').pipe(res);
}
else if(req.url === '/contact'){
res.writeHead(200,{'content-Type': 'text/html'});
fs.createReadStream(__dirname + '/public/contact.html').pipe(res);
}
else if(req.url === '/company'){
res.writeHead(200,{'content-Type': 'text/html'});
fs.createReadStream(__dirname + '/public/company.html').pipe(res);
}
console.log(req.url);
});
app.listen(8900, () => {
console.log("Started on http://localhost:8900");
});