-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
83 lines (59 loc) · 2.17 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"use strict";
const express = require('express');
const bodyParser = require('body-parser');
const faker = require('faker');
const webpackConfig = require('./webpack.config');
const serverLauncher = require('webpack-focus').serverLauncher;
const NB_GENERATED_ENTITY = 10;
let entityJSON = [];//require('./api-mock/notifs.json');
function createEntity(){
return {
uuid: faker.random.uuid(),
name: faker.name.lastName()
};
}
//let entityJSON = [];
for(let i = 0; i < NB_GENERATED_ENTITY; i++){
entityJSON.push(createEntity());
}
console.log('LLLLLLLLLLL')
console.log('%j', entityJSON);
console.log('LLLLLLLLLLL')
const MOCKED_API_PORT = process.env.API_PORT || 9999;
/*****************************************
********* Webpack dev server *************
******************************************/
webpackConfig.externals = undefined; // Remove externals to make the app run in the dev server
serverLauncher(webpackConfig);
/*****************************************
************** Mocked API ****************
******************************************/
const app = express();
const API_ROOT = '/x/entity';
// Middlewares
app.use(function corsMiddleware(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type');
res.header('Access-Control-Allow-Methods', 'POST,GET,OPTIONS,DELETE');
res.header('Content-Type', 'application/json');
next();
});
app.use(bodyParser.json());
app.get(API_ROOT + '/entity', function getAllNotifications(req, res) {
res.json(entityJSON);}
);
app.get(API_ROOT + '/entity/create', function createNotifs(req, res) {
entityJSON.push(createEntity())
res.json(entityJSON);}
);
app.delete(API_ROOT + '/entity', function deleteNotifs(req, res) {
res.json(JSON.stringify(req.body));
});
app.delete(API_ROOT + '/entity/:id', function deleteNotif(req, res) {
res.json({id: req.params.id});
// res.send('DELETE request to homepage'+req.params.id );
});
const server = app.listen(MOCKED_API_PORT, function serverCallback() {
console.log('Mocked entity API listening at http://localhost:%s', MOCKED_API_PORT);
}
);