-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
108 lines (87 loc) · 2.93 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* @Author: Matteo Zambon
* @Date: 2017-02-14 23:21:33
* @Last Modified by: Matteo Zambon
* @Last Modified time: 2017-03-15 22:46:03
*/
'use strict'
// Get Config
const config = require('./config')
// Set isProduction flag
const isProduction = process.env.NODE_ENV === 'production'
// Set server port
const port = process.env.PORT || 8080
// Set server host
const host = process.env.HOST || '0.0.0.0'
// Allow ~ usage when requiring local files
require('require-self-ref')
// Install Marko for Node
require('marko/node-require').install()
// Enable res.marko
require('marko/express')
if(!isProduction) {
// Enable Browser Refresh for Marko
require('marko/browser-refresh').enable()
// Enable Browser Refresh for Lasso
require('lasso/browser-refresh').enable(config.browserRefresh)
}
// Set Lasso Output Directory
config.lasso.outputDir = config.dirs.output
// Configure Lasso
require('lasso').configure(config.lasso)
// Get Express
const express = require('express')
// Initialize Express App
const app = express()
// Lasso Middleware to serve static content
app.use(require('lasso/middleware').serveStatic())
// Routes / Middlewares from config
for(const r in config.routes) {
// Get current route
const route = config.routes[r]
// Ensure method is defaulted to 'use' in case no method is passed
route.method = route.method || 'use'
/**
* Allow method - route - callback
* @param {string} method HTTP Method or 'use'
* @param {string} path Route path
* @param {function} callback Callback for Route
*/
if(route.path && route.callback) {
console.log('route => ' + route.method + ' ' + route.path + ' => callback')
app[route.method](route.path, route.callback)
}
/**
* Allow method - route - page
* @param {string} method HTTP Method or 'use'
* @param {string} path Route path
* @param {string} page Page name
* @param {object} params Parameters object or empty object
*/
else if(route.path && route.page) {
console.log('route => ' + route.method + ' ' + route.path + ' => ' + route.page)
app[route.method](route.path, (req, res) => {
const templatePath = config.dirs.pages + '/' + route.page + '/index.marko'
const template = require(templatePath)
route.params = route.params || {}
route.params.reqParams = req.params
res.marko(template, route.params)
})
}
/**
* Allow method - route - page
* @param {function} middleware Express Middleware
*/
else if(route.middleware) {
const middleware = config.middlewares[route.middleware]
console.log('middleware => ' + route.middleware + ' => callback')
app.use(middleware)
}
}
// Start listening on http://host:port
app.listen(port, host, () => {
console.log('Server started! Try it out:\nhttp://' + host + ':' + port + '/')
if(process.send) {
process.send('online')
}
})