-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (44 loc) · 1.68 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
var path = require('path')
var webpack = require('webpack')
var webpackConfig = require('./webpack.dev.config')
var express = require('express')
var fetcher = require('./server/fetcher')
var app = new express()
var compiler = webpack(webpackConfig)
var storageConfig = require('./storage.config')
var isProduction = process.env.NODE_ENV === 'production'
var port = process.env.PORT || 5000
if (!isProduction) {
var webpackDevMiddleware = require('webpack-dev-middleware')
var webpackHotMiddleware = require('webpack-hot-middleware')
app.use(webpackDevMiddleware(compiler, {noInfo: true, publicPath: webpackConfig.output.publicPath}))
app.use(webpackHotMiddleware(compiler))
}
var distPath = path.join(__dirname, 'dist')
var assetsPath = path.join(__dirname, 'assets')
app.use('/assets', express.static(assetsPath));
app.use('/dist', express.static(distPath));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html')
})
app.get('/photos', function (req, res) {
fetcher.fetchPhotos(storageConfig).then(photos => {
res.json({ photos: photos || [] })
}).catch(err => {
console.error(err)
res.status(500).send({ error: err });
})
})
app.listen(port, function (error) {
if (error) {
console.error(error)
} else {
console.info('==> 🌎 Listening on port %s. Open up http://localhost:%s/ in your browser.', port, port)
console.info('Loaded config file', storageConfig)
Object.keys(storageConfig).forEach(config => {
if (!!storageConfig[config]) {
console.error(`Error: Configuration ${config} is not defined. The app would not work as expected`)
}
});
}
})