Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathserver.js
More file actions
Latest commit
45 lines (38 loc) · 1.3 KB
/
Copy pathserver.js
File metadata and controls
45 lines (38 loc) · 1.3 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
constexpress=require('express');
constapp=express();
constcors=require('cors');
const{ resources }=require("./resources");
constPORT=process.env.PORT||8000;
app.set('view engine','ejs')
app.use(cors());
app.use(express.static('public'));
app.get('/',(req,res)=>{
if(resources){
res.render('index.ejs',{ resources });
}else{
// respond with status 500 if the resources array could not be loaded from resources.js
res.status(500).json({
error: 'Resources were not able to be loaded from resources.js.'
});
}
});
app.get('/api',(req,res)=>{
res.json(resources);
});
app.get('/api/:keyword',(req,res)=>{
constkeyword=req.params.keyword.toLowerCase();
// filter resources array, return items that match query; tag.
constmatches=resources.filter((obj)=>obj.keywords.some(str=>str.toLowerCase().includes(keyword)));
// if matches were found, respond with matches array in JSON format
if(matches.length){
res.json(matches);
}else{
// respond with status 404, no matches were found
res.status(404).json({
error: `No resources were found with the ${keyword} keyword.`
});
}
});
app.listen(PORT,()=>{
console.log(`The 👨🏭 server 🚗 is 🏃♀️ running 👡 on ⚓ port 🐹 ${PORT}, 🛒 better 💅 go 😝 catch 🙀 it! 🍟`);
});