-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.js
More file actions
60 lines (47 loc) · 1.57 KB
/
app.js
File metadata and controls
60 lines (47 loc) · 1.57 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
60
'use strict';
const path = require('path');
const express = require('express');
const exphbs = require('express-handlebars');
const pathConfigs = require('./server/models/path-config.js');
var folder = process.argv[2] || '/src';
var app = express();
// setup express to use handlebars as the templating engine
var hbs = exphbs.create({
defaultLayout: 'main',
layoutsDir: path.join(__dirname, '/server/views/layouts'),
partialsDir: path.join(__dirname, '/server/views/partials')
});
app.set('views', path.join(__dirname, '/server/views'));
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
// use live reload if debug environment
if (folder === '/src') {
app.use(require('connect-livereload')());
}
// setup server for static assets
app.use('/', express.static(path.join(__dirname, folder)));
// setup static server for mock api
app.use('/api', express.static(path.join(__dirname, '/api')));
// setup server urls
app.get('/', function(req, res) {
var urlSections = req.path.split('/');
urlSections = urlSections.filter(function(sectionString) {
return sectionString.length > 0;
});
var urlPath = null;
if (urlSections.length === 0) {
urlPath = '/';
} else {
urlPath = '/' + urlSections[1];
}
var pathConfig = pathConfigs.getConfig(urlPath);
if (!pathConfig) {
res.status(404).send();
return;
}
res.render(pathConfig.data.view, pathConfig);
});
app.listen(3000, () => {
console.log(`Running MaltaJS PWA Demo (${folder}) on localhost:3000`);
});
module.exports = app;