-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (50 loc) · 1.33 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict'
const express = require('express')
const bodyParser = require('body-parser')
const validator = require('./res/validator')
const schemas = require('./res/schemas')
const app = express()
const port = process.env.PORT || '8080'
// Middlewares
app.use(bodyParser.json())
// Routes
app.get('/', (req, res) => {
res.send('this page is working!')
})
app.get('/all', (req, res) => {
res.send(schemas)
})
app.get('/schema/events/:event', (req, res) => {
const event = req.params.event
if (!schemas.events[event]) {
res.send(`${event} is not a valid Segment event type.`)
} else {
res.send(schemas.events[event])
}
})
app.post('/echo', (req, res) => {
res.send(req.body)
})
// Validate Segment events
// TO DO: Should this endpoint be its own API?
app.post('/validate', (req, res) => {
const type = req.body.type
const validate = validator.compile(schemas.events[type])
const valid = validate(req.body)
if (!valid) {
res.send(JSON.stringify(validate.errors.map(err => {
return {
path: err.dataPath,
message: err.message
}
})))
} else {
res.send(`${type} is a valid Segment event!`)
}
})
app.get('/spec', (req, res) => {
res.sendFile(__dirname + '/openapi/index.html')
})
app.listen(port, () => {
console.log(`Listening to requests on http://localhost:${port}`);
})