This repository was archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (48 loc) · 1.29 KB
/
Copy pathserver.js
File metadata and controls
58 lines (48 loc) · 1.29 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
52
53
54
55
56
57
58
const Koa = require('koa')
const Router = require('koa-router')
const cors = require('@koa/cors')
const koaBody = require('koa-body')
const serve = require('koa-static')
const mount = require('koa-mount')
const aconnect = require('./lib/aconnect.js')
const app = new Koa()
const router = new Router()
const assets = new Koa()
assets.use(serve(`${__dirname}/client/dist`))
app.use(mount('/app', assets))
app.use(cors({ origin: '*' }))
app.use(async (ctx, next) => {
try {
await next()
} catch (err) {
ctx.status = err.statusCode || err.status || 500
ctx.body = {
message: err.message
}
}
})
router.get('/', async (ctx, next) => {
ctx.response.status = 200
ctx.body = 'Hi there!'
next()
})
router.get('/midi-devices', async (ctx, next) => {
const devices = await aconnect.getMidiDevices()
// const devices = expected
ctx.response.status = 200
ctx.body = devices
next()
})
router.post('/connect', koaBody(), async (ctx, next) => {
const { sourceId, targetId } = ctx.request.body
await aconnect.connectDevices({ sourceId, targetId })
ctx.response.status = 201
next()
})
router.delete('/disconnect-all', async (ctx, next) => {
await aconnect.disconnectAllDevices()
ctx.response.status = 204
next()
})
app.use(router.routes())
module.exports = app.listen(3000)