-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
250 lines (201 loc) · 6.81 KB
/
index.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import express from "express";
import dotenv from "dotenv";
import path from "path";
import mongoose from "mongoose";
import {nanoid} from "nanoid";
import urlExist from "url-exist";
import URL from "./models/urlModel.js";
import fetch from 'isomorphic-fetch';
import {stringify} from "querystring";
import users from "./models/users.js";
import userURL from "./models/userURL.js";
import alert from 'alert';
import bodyParser from "body-parser";
import session from "express-session";
//const kisisellestirme = document.getElementById("ozel");
const __dirname = path.resolve();
dotenv.config();
const app = express();
app.use(bodyParser.urlencoded());
app.use(express.json());
app.use(session({
secret: 'Özel-Anahtar',
resave: false,
saveUninitialized: true
}));
app.use(express.urlencoded({extended: true}));
app.use(express.static(__dirname + "/public"));
mongoose.connect(process.env.MONGO_DB_URI, (err) => {
if (err) {
console.log("Database bağlantısı başarısız");
throw err;
}else{
console.log("Database bağlantısı başarılı");
}
});
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/index.html");
});
app.get('/admin', async (req,res) => {
res.sendFile("/public/kullanici-dash/index.html", {root: __dirname });
})
const validateURL = async (req, res, next) => {
const {url} = req.body;
const isExist = await urlExist(url);
if (!isExist) {
return res.json({message: "Geçersiz Bağlantı", type: "failure"});
}
next();
};
app.post('/ozi', async (req, res) => {
while (!req.body.captcha)
return res.json({success: false, msg: "Lütfen Captcha'yı seçiniz"});
const secretKey = '6Led7kojAAAAACF0smbT5PfIKMR20OkgscUun';
const query = stringify({
secret: secretKey,
response: req.body.captcha,
remoteip: req.connection.remoteAddress
});
const verifyURL = `https://google.com/recaptcha/api/siteverify?${query}`;
const token = req.get("User-Key");
const body = await fetch(verifyURL).then(res => res.json());
app.post("/link", validateURL, (req, res) => {
const {url} = req.body;
console.log(url);
const {kisi} = req.body;
console.log(kisi);
console.log(req.body);
let shortened_url;
shortened_url = nanoid(10);
console.log(shortened_url);
if(kisi !== undefined ){
console.log("girdi");
shortened_url = kisi;
};
let newURL = new URL({
url,
user_id: req.session.user_id,
shortened_url: (shortened_url),
created_at: new Date(),
});
try {
newURL.save((err, doc) => {
const token = req.get("User-Key");
console.log(token);
let rel = new userURL(
{
user_id: req.session.user_id,
url_id: doc.url,
}
)
rel.save();
});
} catch (err) {
res.send(" Kaydetme sırasında bir hata meydana geldi. Lütfen tekrar deneyin!");
}
res.json({message: `https://url41.herokuapp.com/${newURL.shortened_url}`, type: "success"});
});
});
app.get("/links", async (req, res) => {
const token = req.get("User-Key");
const rel = await userURL.find({user_id: token});
const urls = await URL.find({_id: rel.map((user) => user.url_id)});
res.json(urls);
});
app.get("/:shortened_url", async (req, res) => {
const token = req.get("user_id");
const shortened_url = req.params.shortened_url;
const originalLink = await URL.findOne({shortened_url});
try {
if (originalLink) {
await URL.updateOne(
{
shortened_url: req.params.shortened_url
},
{$inc: {clicks: 1}},
{user_id: token}
);
return res.redirect(originalLink.url);
} else return res.sendFile(__dirname + "/public/404.html");
} catch (err) {
console.log(err);
res.status(500).json('Server Error');
}
});
app.post("/register", async (req, res) => {
let {name, surname, email, password, phone_number} = req.body
users.find({email: email}, (err, data) => {
if (data.length > 0) {
res.json({message: "Bu email adresi kullanımda"});
} else {
let newUser = new users({
name: name,
surname: surname,
email: email,
password: password,
phone_number: phone_number
});
try {
newUser.save();
} catch (err) {
res.send("Kaydetme sırasında bir hata meydana geldi. Lütfen tekrar deneyin!");
}
alert("Kayıt Başarılı!");
res.sendFile('public/giris.html', {root: __dirname });
//res.send({message: "success", user_id: newUser._id});
}
});
});
app.post("/login", async (req, res) => {
let {email, password} = req.body
users.findOne({email: email},(err, data) => {
if (data.length === 0) {
res.send({message: "Bu email adresi kullanımda değil"});
}
password !== data.password ?
res.json({message: "Şifre yanlış"}) :
//res.json({message: "success", user_id: data[0]._id});
req.session.user_id=data._id;
console.log("_id",req.session.user_id);
res.sendFile('public/kullanici-homepage.html', {root: __dirname });
});
});
app.delete("/del/:email", function(req,res){
let delEmail=req.params.email;
users.findOneAndDelete(({email:delEmail}),function(err,docs){
if(err){
res.send("error occured")
}
else{
if(docs==null){
res.send("Yanlış mail adresi girdiniz.")
}
else{
res.send(docs);
}
}
})
});
app.put("/update/:email", async(req,res)=>{
let email=req.params.email;
let updateName=req.body.name;
let updateSurname=req.body.surname;
let updatePassword=req.body.password;
let updatePhone=req.body.phone_number;
users.findOneAndUpdate({email:email},{$set:{name:updateName,surname:updateSurname,password:updatePassword,phone_number:updatePhone}},
{new:true},(err,data)=>{
if(err){
res.send("error occured")
}else{
if(data==null){
res.send("bulunamadı");
}
else{
res.send(data)
}
}
})
});
app.listen(process.env.PORT || 8000, () => {
console.log("server 8000 numaralı portta çalışıyor");
});