-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
47 lines (41 loc) · 1.51 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
require('dotenv').config()
const express = require('express')
const app = express()
const port = process.env.PORT || 3030
const path = require('path')
const axios = require('axios')
app.set('view engine', 'ejs')
app.use(express.static(path.join(__dirname, 'public')))
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.get('/', async (req, res) => {
try {
let api = await axios.get('https://www.breakingbadapi.com/api/characters')
res.render('hp', { api: api, favicon: process.env.favicon })
} catch (error) {
console.log(error.message)
}
})
app.get('/search', async (req, res) => {
try {
const name = req.query.name;
if (name !== undefined) {
name.split('')
let newName = name.replace(' ', '+')
let getName = await axios.get(`https://www.breakingbadapi.com/api/characters?name=${newName}`)
res.render('search', { name: getName.data, favicon: process.env.favicon })
} else {
let allName = await axios.get(`https://www.breakingbadapi.com/api/characters?name=`)
res.render('search', { name: allName.data, favicon: process.env.favicon })
}
} catch (error) {
console.log(error.message);
}
})
app.use((req, res) => {
const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
res.status(404).render('404', { failUrl: fullUrl, favicon: process.env.favicon })
})
app.listen(port, () => {
console.log(`listening on port ${port}`);
})