-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (59 loc) · 1.76 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
// console.log('i am the main entry point, you can run me by typing "node ."')
// packages
const express = require('express')
const axios = require('axios')
// app config
const app = express()
app.set('view engine', 'ejs')
const PORT = 3000
// routes
// GET / -- render a form that lets the user search SWAPI
app.get('/', (req, res) => {
res.render('index.ejs')
})
// GET /search -- take a search from the user and render the results for them to see
// using dot then syntax
app.get('/search', (req, res) => {
console.log(req.query.userInput) // express puts the query strings here
const searchUrl = `https://swapi.dev/api/people/?search=${req.query.userInput}`
axios.get(searchUrl)
.then(response => {
// render the temple once the API gets back to us
res.render('results.ejs', {
people: response.data.results,
input: req.query.userInput
})
})
.catch(console.log) // HANDLE YOUR ERRORS
})
// async/await
app.get('/search', async (req, res) => {
try {
// route logic here
const searchUrl = `https://swapi.dev/api/people/?search=${req.query.userInput}`
const response = await axios.get(searchUrl)
res.render('results.ejs', {
people: response.data.results,
input: req.query.userInput
})
} catch (err) {
// error handling logic here
console.log(err)
}
})
// GET /search/homeworld -- search the url from the query params and render a template
app.get('/search/homeworld', (req, res) => {
// console.log(req.query.url)
axios.get(req.query.url)
.then(response => {
// console.log(response.data)
res.render('homeworld.ejs', { world: response.data })
})
.catch(err => {
console.log('🔥🔥🔥🔥🔥', err)
})
})
// listen for incoming connections
app.listen(PORT, () => {
console.log(`rrrrraaawwwawawaarrrgghggh ${PORT}`)
})