-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
113 lines (104 loc) · 2.66 KB
/
Copy pathindex.js
File metadata and controls
113 lines (104 loc) · 2.66 KB
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
// importing express
var express = require('express')
// import dotenv and configure it
require('dotenv').config()
// importing qrcode
const QRcode = require('qrcode')
// importing uuid
const { v4: uuidV4 } = require('uuid')
// import mongoose
var mongoose = require('mongoose')
// connecting to the database
mongoose.connect(process.env.MONGO + '/QRDB', { useNewUrlParser: true })
// create a URL schema
const urlSchema = mongoose.Schema({
name: String,
url: String,
})
// create a URL model
const URL = new mongoose.model('url', urlSchema)
// creating express router
var router = express.Router()
// creating the root route
router
.route('/')
// get request
.get((req, res, next) => {
// render the homepage of the site
res.render('index')
})
// post request
.post((req, res) => {
// if the request body has a text field
if (req.body.Text) {
// generating a qr code from the text
QRcode.toDataURL(req.body.Text, (err, url) => {
if (!err) {
// rendering the qr code generated
res.render('qrcode', { url: url })
} else {
console.log(err)
}
})
// if the request body has a url field
} else if (req.body['URL']) {
// generating a qr code from the url
QRcode.toDataURL(req.body['URL'], (err, url) => {
if (!err) {
// rendering the qr code generated
res.render('qrcode', { url: url })
} else {
console.log(err)
}
})
// if the request body has a image field
} else {
// generating a random string
var newVar = uuidV4()
// getting the base64 string of the image
var base64Str = req.body.ImageString
// checking if the image already exists
URL.findOne({ name: newVar })
.then((url) => {
if (err) {
console.log(err)
res.send('Error')
} else {
res.send('Already Exist')
}
})
// if the image does not exist then save it to the database
.catch((error) => {
// creating a new URL object
const ur = new URL({
name: newVar,
url: base64Str,
})
// saving the object to the database
ur.save()
// generating a qr code from the url
QRcode.toDataURL(process.env.URL + `/image/${newVar}`, (er, ul) => {
if (!er) {
// rendering the qr code generated
res.render('qrcode', { url: ul })
} else {
console.log(er)
}
})
})
}
})
// creating the image route
router.route('/image/:test').get((req, res) => {
// finding the image in the database
URL.findOne({ name: req.params.test })
.then((rl) => {
// rendering the image
res.render('image', { img: rl.url })
})
.catch((error) => {
res.send('Not Found')
})
})
// exporting the router
module.exports = router