-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.js
41 lines (34 loc) · 965 Bytes
/
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
require('dotenv').config()
const express = require('express')
const path = require('path')
const NounProject = require('the-noun-project')
const app = express()
const np = new NounProject({
key: process.env.NOUN_PROJ_APIKEY,
secret: process.env.NOUN_PROJ_SECRET
})
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '/index.html'))
})
app.get('/icon', (req, res) => {
const maxTries = 3
const handler = (numTry) => {
if (numTry < maxTries) {
const randomId = Math.floor(Math.random() * 1538985)
np.getIconById(randomId, (err, data) => {
if (err) {
handler(numTry++)
} else {
res.json(data)
}
})
} else {
res.status(500).json({error: 'Maximum retry exceeded.'})
}
}
handler(0)
})
const listener = app.listen(process.env.PORT || 3000, () => {
console.log(`Your app is listening on port ${listener.address().port}`)
})