-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
98 lines (80 loc) · 2.97 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
const express = require('express')
const app = express()
require('dotenv').config()
const PORT = process.env.PORT || 5000
const DB_STRING = process.env.DB_STRING
const { MongoClient } = require('mongodb')
let db;
MongoClient.connect(DB_STRING, { useUnifiedTopology: true })
.then(client => {
console.log('connected to db')
db = client.db('find-a-dev')
})
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.get('/', async (req, res) => {
try {
const devs = await db.collection('devs').find().toArray()
res.render('index.ejs', { devs })
} catch (err) {
console.error(err)
}
});
app.get('/findDevs', async (req, res) => {
try {
const devs = await db.collection('devs').find({ skills: req.query.tag }).toArray()
res.render('index.ejs', { devs })
} catch (err) {
console.error(err)
}
})
app.get('/developerForm', (req, res) => {
res.render('addnew.ejs')
})
app.post('/addDeveloper', async (req, res) => {
console.log(req.body)
try {
const unique = await db.collection('devs').find({ name: req.body.name }).toArray()
if (!unique.length) {
console.log('unique dev being added to db')
let skills = ['HTML', 'CSS', 'JavaScript', 'Node', 'MongoDB', 'EJS', 'Handlebars', 'React']
skills = Object.keys(req.body).filter(key => skills.includes(key))
if (!skills.length) {
// default to html/css/js until future implementation
skills = ['HTML', 'CSS', 'JavaScript']
}
let { github, twitter, linkedin } = req.body
// cleansing strings
github.replace('http://github.com/', '')
github.replace('https://github.com/', '')
github.replace('github.com/', '')
github.replace('@', '')
github.trim()
twitter.replace('http://twitter.com/', '')
twitter.replace('https://twitter.com/', '')
twitter.replace('twitter.com/', '')
twitter.replace('@', '')
twitter.trim()
linkedin.replace('http://linkedin.com/in/', '')
linkedin.replace('https://linkedin.com/in/', '')
linkedin.replace('linkedin.com/in/', '')
linkedin.replace('@', '')
linkedin.trim()
db.collection('devs').insertOne({
name: req.body.name.trim(),
avatar: `https://github.com/${github}.png`,
skills: skills,
expertise: req.body.expertise,
twitter: `https://twitter.com/${twitter}`,
linkedin: `https://linkedin.com/in/${linkedin}`,
github: `https://github.com/${github}`,
})
}
res.redirect('/')
} catch (err) {
console.error(err)
}
})
app.listen(PORT, () => console.log(`server running on http://localhost:${PORT}`))