-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
145 lines (131 loc) · 3.42 KB
/
app.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const fs = require('fs');
const mongoose = require('mongoose');
// const pass = process.env.ABC;
// console.log(pass);
// mongoose.connect("mongodb://127.0.0.1:27017/fish",{useNewUrlParser:true});
const connection_string = process.env.CONNECTION_STRING;
console.log(connection_string);
mongoose.connect(connection_string);
const app = express();
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static("public"));
app.set('view engine','ejs');
const fishSchema = new mongoose.Schema({
name:String,
knownAs:String,
scientificName:String,
appearance:String,
habitat: String,
diet: String,
nutritionValue: String,
idx:Number,
photo: Buffer
});
const Fish = new mongoose.model("Fish",fishSchema);
// let ph = fs.readFileSync(`C:/Users/SHREYASH/Desktop/download (26).jpeg`);
// Fish.updateOne({idx:6},{$set:{photo:ph}},{overwrite:true},function(err){
// if(!err){
// console.log("Successfully updated");
// }
// });
// console.log(ph);
let curr_pred;
app.post('/api/sendParagraphValue', function(req, res) {
const { paragraphValue } = req.body;
console.log(req.body);
res.send("Done");
});
app.post('/api',function(req,res){
// console.log(req.body);
curr_pred = req.body.pred;
// console.log(curr_pred);
Fish.findOne({idx:curr_pred},function(err,foundFish){
if(!err){
if(foundFish){
fishFound=foundFish;
// console.log(foundFish);
res.render("pred-fish",{fish:fishFound});
}
else{
res.send("No data found! Contact Developer");
}
}
else{
console.log(err);
}
});
});
app.post("/register",function(req,res){
console.log(req.body.name);
console.log(req.body);
const fish = new Fish({
name: req.body.name,
knownAs:req.body.knownAs,
scientificName:req.body.scientificName,
appearance:req.body.appearance,
habitat:req.body.habitat,
diet : req.body.diet,
nutritionValue:req.body.nutritionValue,
idx:req.body.idx
});
fish.save(function(err){
if(!err){
console.log("submitted");
}
else{
console.log(err);
}
});
res.redirect('/register');
});
app.get("/",function(req,res){
res.render("index",{name:"Shreyash Gawande"});
});
app.get("/Category",function(req,res){
Fish.find({},function(err,foundFishes){
if(!err){
if(foundFishes){
res.render("cards",{foundFishes:foundFishes});
}
else{
res.send("No data found");
}
}
else{
console.log(err);
}
});
});
let fishFound;
app.get("/fish/:fishID",function(req,res){
const fishId = req.params.fishID;
console.log(fishId);
Fish.findOne({idx:fishId},function(err,foundFish){
if(!err){
if(foundFish){
fishFound=foundFish;
console.log(foundFish);
res.render("fish",{fish:fishFound});
}
else{
res.send("No data found! Contact Developer");
}
}
else{
console.log(err);
}
});
});
app.get("/fish",function(req,res){
res.render("fish",{fish:fishFound});
});
app.get("/register",function(req,res){
res.render("form");
});
app.listen(3000,function(){
console.log("server is running on localhost 3000" + curr_pred);
})