-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevServerHapi.js
56 lines (51 loc) · 1.3 KB
/
devServerHapi.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
const path = require('path');
const Server = require('hapi').Server;
const Webpack = require('webpack');
const WebpackPlugin = require('hapi-webpack-plugin');
const config = require('./webpack.config.dev');
const Inert = require('inert');
const server = new Server();
server.connection({
port: 8080,
routes: {
cors: true,
json: {space: 2}
}
});
server.register(Inert, () => {
});
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: path.join(__dirname, 'public')
}
}
});
server.ext('onPreResponse', (request, reply) => {
if (request.response.isBoom && request.response.output.statusCode === 404) {
// use index.html as fallback in case of 404 errors (esp to enable React Router with BrowserHistory)
// (something like Webpack Devserver's --history-api-fallback option)
return reply.file(path.join(__dirname, 'public/index.html'));
}
return reply.continue();
});
const compiler = new Webpack(config);
const assets = {
noInfo: true,
publicPath: config.output.publicPath
};
const hot = {};
server.register(
{
register: WebpackPlugin,
options: {compiler, assets, hot}
},
error => {
if (error) {
return console.error(error);
}
server.start(() => console.log('Server running at:', server.info.uri));
}
);