-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
202 lines (179 loc) · 4.88 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
require('dotenv').config();
var express = require('express') //necessary modules required
var router = express.Router();
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
var nodemailer = require('nodemailer');
var path = require('path')
var session = require('express-session');
var multer = require('multer');
var alert = require('alert-node')
var fs = require('fs');
const mongodb = require('mongodb');
var app = express();
//body parser and database connectivity
app.use(express.static(path.join(__dirname,'public')));
app.use(express.urlencoded({extended: true})); //If extended is false, you can not post "nested object"
app.use(express.json()); //incoming request as a json object
app.use(function (req, res, next) { //middleware function that have access to request,response and next obj
next()
})
var mongoose = require('mongoose');
var schema = mongoose.Schema;
var ngodb = 'mongodb://localhost/NGOs';
mongoose.connect(ngodb);
mongoose.connection.on('error',(err) => {
console.log('DB connection Error');
})
mongoose.connection.on('connected',(err) => {
useNewUrlParser: true;
console.log('DB connected');
})
//Schemas Defined
//mailing schema
var mailSchema = new mongoose.Schema({
firstName: String,
lastName: String,
username: String,
subject: String,
message: String,
})
//NGO Schema
var ngoSchema = new mongoose.Schema({
nGOName: String,
filingPersonName: String,
email: String,
address: String,
statengo: String,
employement: String,
experience: String,
contactno: String,
})
//Victim Schema
var VictimSchema = new mongoose.Schema({
victim_name: String,
gender: String,
age: String,
contactno: String,
email: String,
address: String,
description: String,
statename: String,
issueFacing: String,
abuse_status: String,
extent_abuse: String,
criminalName: String,
relationWithCriminal: String,
})
//RF Schema
var ReportAsAFriendSchema = new mongoose.Schema({
rfName: String,
age: String,
contactno: String,
email: String,
address: String,
victim_name: String,
victim_number: String,
relationshipWithVictim: String,
description: String,
})
var ngo = mongoose.model('ngo', ngoSchema);
var victim = mongoose.model('victims',VictimSchema);
var reporter = mongoose.model('friendreport',ReportAsAFriendSchema);
var mailbox = mongoose.model('mails',mailSchema);
app.post('/mail',function(req,res){
var obj=req.body;
mailbox.create(obj,function(error,result){
if(error)
throw err;
else{
res.sendFile(path.join(__dirname + '/public/index.html'));
}
})
})
app.post('/addNgo',function(req,res) //Add NGO Request
{
var obj = req.body;
ngo.create(obj,function(error,result)
{
if(error)
throw err;
else
{
res.sendFile(path.join(__dirname + '/public/index.html')); }
})
})
app.post('/addVictim',function(req,res) //Add Victim Request
{
var obj = req.body;
victim.create(obj,function(error,result)
{
if(error)
throw err;
else
{
res.sendFile(path.join(__dirname + '/public/index.html')); }
})
})
app.post('/Rf',function(req,res) //Friend Report Request
{
var obj = req.body;
reporter.create(obj,function(error,result)
{
if(error)
throw err;
else
{
res.sendFile(path.join(__dirname + '/public/index.html')); }
})
})
app.post('/mail',function(req,res){
var obj = req.body;
let transporter = nodemailer.createTransport({
service :'gmail',
auth : {
user : process.env.EMAIL,
pass : process.env.PASSWORD
}
});
let mailOptions = {
from : '[email protected]',
to : '[email protected]',
subject : 'Shoutup Contact Request',
text : "You have got a contact request from: " + obj.username + "\n" + " From SHOUTUP\nThe First Name is: " + obj.firstName + "\n" + " Last Name is: " + obj.lastName + "\nThe subject is: " + obj.subject + "\nThe message is: " + obj.message
};
transporter.sendMail(mailOptions,function(err,data){
if (err) {
console.log('Error Occurs');
} else {
console.log('Email sent!!');
res.sendFile(path.join(__dirname + '/public/index.html'));
}
});
});
//image uploading code
var storage=multer.diskStorage({
destination : function(req,file,cb){
cb(null,'uploads')
},
filename : function(req,file,cb){
cb(null,file.fieldname + '-' + Date.now() + path.extname(file.originalname))
}
})
var upload = multer({
storage : storage
})
app.post('/uploadphoto',upload.single('myImage'),(req,res) => {
var img = fs.readFileSync(req.file.path);
var encode_image = img.toString('base64');
var finalImg = {
contentType : req.file.mimetype,
path : req.file.path,
image : new Buffer(encode_image,'base64')
};
})
//Server Running Confirmation
app.listen(4400,function()
{
console.log("Running on port 4400");
});