-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
85 lines (71 loc) · 2.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
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
var express = require('express');
var fetchAction = require('node-fetch');
var mustacheExpress = require('mustache-express');
var app = express();
var url = "https://data.combatant32.hasura-app.io/v1/query";
var requestOptions = {
"method": "POST",
"headers": {
"Content-Type": "application/json"
}
};
var body = {
"type": "select",
"args": {
"table": "krishiplusplus",
"columns": [
"*"
],
"where": {
"shnumber": {
"$eq": ""
}
}
}
};
app.engine('mustache', mustacheExpress());
app.set('view engine', 'mustache');
app.set('views', __dirname + '/views');
app.use(express.static('public'));
app.get('/query', function (req, res) {
var response = {
shnumber:req.query.shnumber,
pincode:req.query.pincode
};
body.args.where.shnumber.$eq = response.shnumber;
requestOptions.body = JSON.stringify(body);
fetchAction(url, requestOptions)
.then(function(response) {
return response.json();
})
.then(function(result) {
if (result.length <= 0) {
res.sendFile( __dirname + "/" + "error.html" );
} else {
res.render('index', {
name: result[0].name,
crop: result[0].crop,
variety: result[0].variety,
intercrop: result[0].intercrop,
sowingPeriod: result[0].sowingPeriod,
landprep: result[0].landprep,
seedrate: result[0].seedrate,
spacing: result[0].spacing,
fertilizer: result[0].fertilizer,
state: result[0].state,
yield: result[0].yield,
flood: result[0].flood,
cyclone: result[0].cyclone,
userimage: result[0].userimage
});
}
})
.catch(function(error) {
console.log('Request Failed:' + error);
});
})
var server = app.listen(8080, function(){
//var host = server.address().address
var port = server.address().port;
console.log("Example express app at port: %s", port);
})