-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
69 lines (55 loc) · 1.72 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
const { join } = require('path')
const { existsSync } = require('fs')
const express = require('express')
const engines = require('consolidate')
const favicon = require('serve-favicon')
const db = require('./persistence/db')
const app = express()
app.use(favicon(join(__dirname, 'public/favicon.ico')))
app.use(require('cookie-parser')())
// configure views
app.set('views', join(__dirname, 'views'))
app.set('view engine', 'hbs')
app.engine('hbs', engines.handlebars)
app.set('x-powered-by', false)
app.set('json spaces', 2)
// local dev only
if (!process.env.PORT) {
var config = require('./webpack.dev')
var compiler = require('webpack')(config)
app.use(
require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath,
}),
)
}
app.use('/public', express.static('public'))
app.use('/preview', require('./routes/preview'))
app.use('/api', require('./routes/api'))
app.use('/', require('./routes/edit'))
// serve assets requested by previews
app.use('/:owner/:name/:asset', (req, res, next) => {
const { owner, name, asset } = req.params
const dir = join(__dirname, 'tmp', owner, name)
if (!existsSync(dir)) {
return res.status(404).send('Project does not exist.')
}
if (!asset.includes('.')) {
return next()
}
res.setHeader('Cache-Control', 'no-cache')
res.sendFile(join(dir, asset))
})
// the React app is the fallback for all routes
app.use(function(req, res) {
res.sendFile(join(__dirname, 'routes', 'edit', 'index.html'))
})
module.exports = function(local, port) {
port = port || process.env.PORT || 3000
db.open().then(function() {
app.listen(port, function() {
console.log('Server listening at http://literasee.local:' + port)
})
})
}