-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·117 lines (105 loc) · 4.21 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
const http = require('http');
const port = process.env.PORT || 3000;
const express = require('express')
const bodyParser= require('body-parser')
const app = express();
const ejsLint = require('ejs-lint');
var urlencodedParser = app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/assets'));
var database;
var path = require('path');
const mongoClient = require('mongodb').MongoClient;
// gridfs packages
var mongoose = require('mongoose');
var Grid = require("gridfs-stream");
Grid.mongo = mongoose.mongo;
var ObjectID = require('mongodb').ObjectID;
var Base64Encode = require('base64-stream');
var fs = require('fs');
// connecting to mongodb
var url = "mongodb://ks4049:project123@ds227332.mlab.com:27332/project";
var url1 = "mongodb://ks4049:project123@ds157493.mlab.com:57493/gridfs";
//var url = "mongodb://localhost:27017/";
//var url1 = "mongodb://localhost:27017/gridfs";
mongoose.connect(url1);
var conn = mongoose.connection;
mongoClient.connect(url, function(err, client){
if (err) return console.log(err);
console.log("Connecting.."+client);
database = client.db("project", function(err, db){
if (err) throw err;
console.log("Connected to db");
});
});
// the home page
app.get('/',function(req, res){
res.sendFile(path.join(__dirname+'/views/index.html'));
})
//Get all restaurants based on search
app.get('/restaurants', (req, res) => {
var searchText = req.query.searchText;
var searchParam =req.query.searchParam;
if(searchParam == "all"){
var regexText = new RegExp(".*"+searchText+".*", 'i');
var findQuery = [{"Restaurant Name" : regexText}, {"Restaurant ID": regexText}, {"Country": regexText}, {"Cuisines": regexText}, {"Address": regexText}];
database.collection('restaurants').find({$or: findQuery}).toArray(function(err, results){
if(err) throw err;
res.send(results);
});
}
else{
database.collection('restaurants').find({[searchParam]: new RegExp(".*"+searchText+".*", 'i')}).toArray(function(err, results){
if(err) throw err;
res.send(results);
});
}
});
//Get the restaurant by ID
app.get('/getRestaurantById',function(req, res){
var restID = parseInt(req.query["Restaurant ID"]);
database.collection('restaurants').findOne({"Restaurant ID": restID}, function(err, result){
if(err) throw err;
//get the restaurant Image
var restaurantImg = result["Image ID"];
var gfs = Grid(conn.db);
gfs.collection("images").findOne({"_id": restaurantImg}, function(err, imageFile){
if (err) throw err;
var readstream = gfs.createReadStream({_id: imageFile["_id"]});
readstream.on('data', (chunk) => {
res.render("displayRestaurant.ejs", {restaurantInfo: result, image: chunk.toString('base64') });
})
});
});
});
//Add review for restaurants
app.post('/addReviewToRestaurant', function(req, res){
var reviewText = req.body["reviewText"];
var restaurantId = ObjectID(req.query["_id"]);
var updateOperation = {$push:{"comments": reviewText}};
//add comments
var updateStatus = database.collection("restaurants").updateOne({"_id":restaurantId},updateOperation, function(err, result){
if (err) throw err;
console.log("Updated successfully");
res.send(result);
});
});
//get Restaurants based on Location
app.get('/getRestaurantsByLocation',function(req, res){
var longit = parseFloat(req.query.longitude);
var lat = parseFloat(req.query.latitude);
var longLat = [];
longLat.push(longit);
longLat.push(lat);
var searchText = req.query.searchText;
var regexText = new RegExp(".*"+searchText+".*", 'i');
var findQuery = [{"Restaurant Name" : regexText}, {"Restaurant ID": regexText}, {"Country": regexText}, {"Cuisines": regexText}, {"Address": regexText}];
var geoQuery = {"loc": {$near:{$geometry:{"type":"Point", "coordinates": longLat}, $maxDistance: 500000}}};
//database.collection('restaurants').createIndex({point:"2dsphere"});
var cursor = database.collection('restaurants').find({$and:[geoQuery, {$or:findQuery}]}).toArray(function(err, results){
if (err) throw err;
res.send(results);
});
});
app.listen(port, () => console.log(`Listening on port: ${port}`));
console.log('Search based application');