forked from nwah/peerjs-audio-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
52 lines (43 loc) · 1.37 KB
/
routes.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
var express = require('express');
var router = express.Router();
var config = require('./config');
var Call = require('./call');
// Create a new Call instance, and redirect
router.get('/new', function(req, res) {
var call = Call.create();
res.redirect('/' + call.id);
});
// Add PeerJS ID to Call instance when someone opens the page
router.post('/:id/addpeer/:peerid', function(req, res) {
var call = Call.get(req.param('id'));
if (!call) return res.status(404).send('Call not found');
call.addPeer(req.param('peerid'));
res.json(call.toJSON());
});
// Remove PeerJS ID when someone leaves the page
router.post('/:id/removepeer/:peerid', function(req, res) {
var call = Call.get(req.param('id'));
if (!call) return res.status(404).send('Call not found');
call.removePeer(req.param('peerid'));
res.json(call.toJSON());
});
// Return JSON representation of a Call
router.get('/:id.json', function(req, res) {
var call = Call.get(req.param('id'));
if (!call) return res.status(404).send('Call not found');
res.json(call.toJSON());
});
// Render call page
router.get('/:id', function(req, res) {
var call = Call.get(req.param('id'));
if (!call) return res.redirect('/new');
res.render('call', {
apiKey: config.peerjs.key,
call: call.toJSON()
});
});
// Landing page
router.get('/', function(req, res) {
res.render('index');
});
module.exports = router;