Merge pull request #126 from geek/hapi

Use hapi instead of express
This commit is contained in:
Tom Gallacher 2017-01-03 11:32:20 +00:00 committed by GitHub
commit da9a3dc8b7
3 changed files with 67 additions and 51 deletions

View File

@ -20,10 +20,10 @@
}, },
"dependencies": { "dependencies": {
"constant-case": "^2.0.0", "constant-case": "^2.0.0",
"express": "^4.14.0",
"force-array": "^3.1.0", "force-array": "^3.1.0",
"hapi": "^16.1.0",
"inert": "^4.1.0",
"inherits": "^2.0.3", "inherits": "^2.0.3",
"locale": "^0.1.0",
"lodash.find": "^4.6.0", "lodash.find": "^4.6.0",
"lodash.flatten": "^4.4.0", "lodash.flatten": "^4.4.0",
"lodash.get": "^4.4.2", "lodash.get": "^4.4.2",
@ -48,8 +48,8 @@
"redux-promise-middleware": "^4.2.0", "redux-promise-middleware": "^4.2.0",
"redux-thunk": "^2.1.0", "redux-thunk": "^2.1.0",
"reselect": "^2.5.4", "reselect": "^2.5.4",
"st": "^1.2.0",
"styled-components": "^1.2.1", "styled-components": "^1.2.1",
"understood": "^1.0.1",
"url-loader": "^0.5.7" "url-loader": "^0.5.7"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,44 +1,71 @@
const template = require('lodash.template'); 'use strict';
const locale = require('locale');
const path = require('path');
const express = require('express');
const st = require('st');
const fs = require('fs');
const app = express(); const fs = require('fs');
const hapi = require('hapi');
const inert = require('inert');
const path = require('path');
const template = require('lodash.template');
const understood = require('understood');
const index = path.join(__dirname, './index.html'); const index = path.join(__dirname, './index.html');
const html = template(fs.readFileSync(index, 'utf-8')); const html = template(fs.readFileSync(index, 'utf-8'));
var mount = st({ const server = new hapi.Server({
path: path.join(__dirname, '../static'), connections: {
url: 'static/', routes: {
index: false, files: {
dot: false, relativeTo: path.join(__dirname, '../static')
passthrough: false, }
gzip: true, }
cors: false }
});
server.connection({
port: process.env.PORT || 8000
}); });
app.use(mount); server.register([
app.use(locale(require('./locales'))); inert,
{
app.get('/*', (req, res, next) => { register: understood,
const locale = (req.locale || '').toLowerCase(); options: {
const lang = locale.split(/\-/)[0]; default: 'en-us', localesDir: path.join(__dirname, '../static/locales')
}
res.header('Content-Type', 'text/html'); }],
(err) => {
res.send(html({
locale,
lang
}));
});
app.listen(8000, (err, address) => {
if (err) { if (err) {
throw err; console.error(err);
process.exit(1);
} }
console.log('Server running at: http://localhost:8000'); server.route({
}); method: 'GET',
path: '/static/{param*}',
handler: {
directory: {
path: '.',
redirectToSlash: true,
index: false
}
}
});
server.route({
method: '*', path: '/{param*}', handler: defaultHandler
});
server.start((err) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`Server running at: http://localhost:${server.info.port}`);
});
});
function defaultHandler(request, reply) {
const locales = (request.locale || '').toLowerCase().split(/\-/);
reply(html({
locale: locales[1], lang: locales[0]
}));
}

View File

@ -1,11 +0,0 @@
const uniq = require('lodash.uniq');
const fs = require('fs');
const path = require('path');
const files = fs.readdirSync(path.join(__dirname, '../static/locales'));
module.exports = uniq(files.map((file) => {
return file.replace(/\.js$/, '');
}).filter((file) => {
return file.match(/.*?-.*?/);
}));