-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
67 lines (54 loc) · 1.73 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
'use strict';
const catberry = require('catberry');
const http = require('http');
const path = require('path');
const express = require('express');
const templateEngine = require('catberry-handlebars');
const Todos = require('./lib/Todos');
// configuration
const config = {
title: 'Catberry • TodoMVC',
server: {
port: Number(process.env.PORT) || 3000
},
logger: {
level: Number(process.env.CAT_LOG_LEVEL) || 30
},
isRelease: process.argv[2] === 'release',
componentsGlob: 'catberry_components/**/cat-component.json',
publicDirectoryPath: path.join(__dirname, 'public')
};
const cat = catberry.create(config);
const app = express();
const logger = require('catberry-logger');
logger.register(cat.locator);
const READY_MESSAGE = 'Ready to handle incoming requests on port';
templateEngine.register(cat.locator);
cat.locator.register('todosHelper', Todos);
const compression = require('compression');
const zlib = require('zlib');
app.use(compression({
flush: zlib.Z_PARTIAL_FLUSH
}));
const serveStatic = require('serve-static');
app.use(serveStatic(config.publicDirectoryPath));
// serving client config
// CREATE A SEPARATE OBJECT HERE AND COPY REQUIRED VALUES
// FROM THE SERVER CONFIG EXCLUDING PRIVATE DATA IF NEEDED
const clientConfig = {
title: config.title,
isRelease: config.isRelease,
logger: config.logger
};
const configResp = `window.$catConfig=${JSON.stringify(clientConfig)}`;
app.get('/config.js', (req, res) => res.end(configResp));
app.use(cat.getMiddleware());
const errorhandler = require('errorhandler');
app.use(errorhandler());
cat.events.on('ready', () => {
const logger = cat.locator.resolve('logger');
logger.info(`${READY_MESSAGE} ${config.server.port}`);
});
http
.createServer(app)
.listen(config.server.port);