-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkarma.api-client-test-server.plugin.js
More file actions
59 lines (44 loc) · 1.52 KB
/
Copy pathkarma.api-client-test-server.plugin.js
File metadata and controls
59 lines (44 loc) · 1.52 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
59
/* eslint-env node */
const express = require('express');
const bodyParser = require('body-parser');
// from example
// https://github.com/tasubo/karma-express-http-server
// https://github.com/weblogixx/karma-restify-server
function createApiClientTestServer(args, config, logger, helper) {
var log = logger.create('api-client-test-server');
log.info('Creating');
var app = express();
app.set('port', 3022);
// ## CORS middleware
function allowCrossDomain(req, res, next) {
if (!req.get('Origin')) return next();
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers',
'Content-Type, Authorization, Content-Length, X-Requested-With');
// intercept OPTIONS method
if ('OPTIONS' == req.method) return res.sendStatus(200);
next();
}
app.use(allowCrossDomain);
// for parsing application/json
app.use(bodyParser.json());
// for parsing application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', function(req, res) {
res.json({ user: 'tobi' });
});
app.get('/users', function(req, res) {
res.json([{ user: 'tobi' }, { user: 'loki' }]);
});
app.post('/users', function(req, res) {
log.info(req.body);
res.json(req.body);
});
app.listen(app.get('port'), () => {
log.info(`Started on port ${app.get('port')}`);
});
}
module.exports = {
'framework:api-client-test-server': ['factory', createApiClientTestServer],
};