-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
221 lines (194 loc) · 6.28 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//Dependencies
var express = require('express');
var app = express();
var path = require('path');
let mongoose = require('mongoose');
var session = require('express-session');
const dotenv = require('dotenv');
dotenv.load();
var mongo = require('mongodb').MongoClient
var database = require('./database.js')
var bodyParser = require("body-parser")
var cookieParser = require("cookie-parser")
var header = require("./header.js")
let db = require('./api/models/db');
//port number
var url = process.env.DATABASE_URI;
var port = process.env.PORT || 3000
mongoose.connect(process.env.DATABASE_URI);
//Set View engine and add static directory
app.set('view engine', 'ejs')
app.set('views', __dirname+'/views')
app.use(express.static(path.join(__dirname, 'public')))
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.use(cookieParser())
app.use(session({ secret: 'asdasdasdawdadad213343qweqwec123', resave: true, saveUninitialized: false }))
app.use((req, res, next) => {
let ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
let headers = req.headers['user-agent'];
let data = {
ip: ip,
useragent: headers,
page: req.url
}
db.addVisitor(data, (err, success) => {
if(err)
db.addLog({message: 'Failed to add User Activity'});
else
db.addLog({message: 'User Activity added'});
})
next();
})
//Homepage view
app.get('/', function(req, res, next){
if(req.session.user){
res.render('client/dashboard', {data: JSON.stringify(req.session.user)})
}
else{
res.render('index')
}
})
//Getting-started view
app.get('/getting-started', function(req, res, next){
res.render('get-started')
})
//Custom links View
app.get('/track-links', function(req, res, next){
res.render('custom')
})
//About Us View
app.get('/about', function(req, res, next){
res.render('aboutus')
})
var originalUrl = ""
//new url view
app.get('/new', function(req, res, next){
originalUrl = req.query['url']
var json_data = {}
//Connect to Database
mongo.connect(url, function(err, db){
if(err)
throw err
var collection = db.collection('urlData')
//Find if the entered URL is already present or not
collection.find({
"original-url": originalUrl
}).toArray((err, document) => {
if(err)
throw err
else{
if(database.exists(document)){
json_data = {
"original link": document[0]['original-url'],
"short link":document[0]['short-url']
}
//console.log("DATA IF = "+document)
}else{
var shortUrl = "http://hurls.in/"
shortUrl += database.generateRandom()
//else insert the new URL into database
collection.insert({
"original-url": originalUrl,
"short-url": shortUrl,
}, function(err, data){
if(err)
throw err
console.log("data inserted "+document)
})
//Get the data
json_data = {
"original link": originalUrl,
"short link": shortUrl
}
}
db.close()
//render result view
res.render('result', {'data': json_data})
}
})
})
})
//User LogIn
app.post("/login", function(req, res, next){
database.loginUser(req.body, function(msg, data){
console.log(msg)
req.session.user = data;
res.send(JSON.stringify({"error": msg, "success": data}))
})
});
app.post('/logout', function(req, res, next){
req.session.user = null;
if(req.session.user)
res.send('err');
else
res.send('done');
})
//User Registration
app.post('/signup', function(req, res, next){
//console.log(req.body)
database.registerUser(req.body, function(msg){
console.log(msg)
res.send(msg)
})
})
//User link add functionality
app.post('/new', function(req, res, next){
console.log(req.body)
database.addNewLink(req.body, function(msg){
console.log(msg)
res.send(msg)
})
})
//Get all URL's of a User
app.post('/geturls', function(req, res, next){
console.log(req.body)
database.getUserLinks(req.body, function(data){
res.send(data)
})
})
//User deletes a URL
app.post('/deleteURL', function(req, res, next){
console.log(req.body)
database.deleteUrl(req.body, function(success){
res.send(success)
})
})
//Redirection functionality
app.get('/*', function(req, res, next){
var requestedUrl = req.url
//console.log(requestedUrl)
mongo.connect(process.env.DATABASE_URI, function(err, db){
if(err)
throw err
db.collection("urlData").find({
"short-url": "http://hurls.in"+requestedUrl
}).toArray(function(err, data){
if(database.exists(data)){
res.redirect(data[0]['original-url'])
}else{
//masking functionality
db.collection("maskedurlData").find({
"masked-url": "http://hurls.in"+requestedUrl
}).toArray(function(err, data){
if(database.exists(data)){
var original_url = data[0]['original-url']
header.getBody(original_url, function(result){
res.render('mask', {data: result})
})
}else{
res.send("ERROR - 404")
}
db.close()
})
}
db.close()
})
})
})
app.listen(port, () => {
console.log("Server is LIVE at "+port)
})