-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
75 lines (58 loc) · 2.1 KB
/
Copy pathserver.js
File metadata and controls
75 lines (58 loc) · 2.1 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var express, path, fs, httpProxy;
express = require('express');
path = require('path');
fs = require('fs');
httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();
var app = express();
var isProduction = process.env.NODE_ENV === 'production';
var port = isProduction ? 8080 : 3001;
var publicPath = path.resolve(__dirname, 'app');
app.use(express.static(publicPath));
app.get('/timestamp.json', function(req, res) {
fs.readFile('dist/timestamp.json', function(err, data) {
res.setHeader('Content-Type', 'application/json');
res.send(data);
});
});
app.get('/images/banner_image/:img', function (req, res) {
var img = fs.readFileSync('./static/images/banner_image/' +
req.params.img);
res.writeHead(200, {'Content-Type': 'image/jpg'});
res.end(img, 'binary');
});
app.get('/images/homepage/:img', function (req, res) {
var fileName, extension, contentType, img;
fileName = req.params.img;
extension = fileName.split('.');
extension = extension[extension.length - 1];
contentType = 'image/' + extension;
img = fs.readFileSync('./static/images/homepage/' +
req.params.img);
res.writeHead(200, {'Content-Type': contentType});
res.end(img, 'binary');
});
// We only want to run the workflow when not in production
if (!isProduction) {
// We require the bundler inside the if block because
// it is only needed in a development environment. Later
// you will see why this is a good idea
var bundle = require('./server/bundle.js');
bundle();
// Any requests to localhost:3000/build is proxied
// to webpack-dev-server
app.all('/*', function (req, res) {
proxy.web(req, res, {
target: 'http://localhost:8080'
});
});
}
// It is important to catch any errors from the proxy or the
// server will crash. An example of this is connecting to the
// server when webpack is bundling
proxy.on('error', function(e) {
console.log('Could not connect to proxy, please try again...');
});
app.listen(port, function () {
console.log('Server running on port ' + port);
});