forked from webgme/webgme
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExampleRestRouter.js
59 lines (43 loc) · 1.46 KB
/
ExampleRestRouter.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
/*jshint node:true*/
/**
* @author lattmann / https://github.com/lattmann
*/
'use strict';
var express = require('express'),
router = express.Router();
function initialize(middlewareOpts) {
var logger = middlewareOpts.logger.fork('ExampleRestRouter'),
//gmeConfig = middlewareOpts.gmeConfig,
ensureAuthenticated = middlewareOpts.ensureAuthenticated,
getUserId = middlewareOpts.getUserId;
logger.debug('initializing ...');
// ensure authenticated can be used only after this rule
router.use('*', function (req, res, next) {
// TODO: set all headers, check rate limit, etc.
res.setHeader('X-WebGME-Media-Type', 'webgme.v1');
next();
});
// all endpoints require authentication
router.use('*', ensureAuthenticated);
router.get('/getExample', function (req, res/*, next*/) {
var userId = getUserId(req);
res.json({userId: userId, message: 'get request was handled'});
});
router.patch('/patchExample', function (req, res/*, next*/) {
res.sendStatus(200);
});
router.post('/postExample', function (req, res/*, next*/) {
res.sendStatus(201);
});
router.delete('/deleteExample', function (req, res/*, next*/) {
res.sendStatus(204);
});
router.get('/error', function (req, res, next) {
next(new Error('error example'));
});
logger.debug('ready');
}
module.exports = {
initialize: initialize,
router: router
};