-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (39 loc) · 1.47 KB
/
Copy pathserver.js
File metadata and controls
51 lines (39 loc) · 1.47 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
const express = require('express') //require express
const app = express() //so dont need to quote express all the time
const PORT =8000
const rappers = {
'21 savage':{
'age': 29,
'birthName': 'SBAJ',
'birthLocation' : 'London, England'
},
'chance the rapper':{
'age': 29,
'birthName':'Chancelor Bennett',
'birthLocation':'Chicago, Illinois'
},
'unknown':{
'age': 0,
'birthName':'unknown',
'birthLocation':'unknown'
}
}
app.get('/', (request, response)=> { //get method
response.sendFile(__dirname + '/index.html') // loooks like an event listner -> instead of click, network request
//__dirname --> just start looking from there no matter where you are
})
app.get('/api/:name', (request, response)=> { //name is the parameter name-doesnt matter to be name or xxxx
const rapperName = request.params.name.toLowerCase()
//console.log(rappers[rapperName]) // to see if the api can show up the right parameter
// request.params.name -> whatever the api show up with we can grab after api
//console.log(request.params.name) --> the terminal will show out
//response.json(rappers)
if ( rappers[rapperName] ){
response.json(rappers[rapperName])
}else{
response.json(rappers['unknown'])
}
})
app.listen(PORT, ()=> {
console.log(`The server is now running on port ${PORT}! Betta Go Catch It!`)
})