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

View File

@ -1,44 +1,71 @@
const template = require('lodash.template');
const locale = require('locale');
const path = require('path');
const express = require('express');
const st = require('st');
const fs = require('fs');
'use strict';
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 html = template(fs.readFileSync(index, 'utf-8'));
var mount = st({
path: path.join(__dirname, '../static'),
url: 'static/',
index: false,
dot: false,
passthrough: false,
gzip: true,
cors: false
const server = new hapi.Server({
connections: {
routes: {
files: {
relativeTo: path.join(__dirname, '../static')
}
}
}
});
server.connection({
port: process.env.PORT || 8000
});
app.use(mount);
app.use(locale(require('./locales')));
app.get('/*', (req, res, next) => {
const locale = (req.locale || '').toLowerCase();
const lang = locale.split(/\-/)[0];
res.header('Content-Type', 'text/html');
res.send(html({
locale,
lang
}));
});
app.listen(8000, (err, address) => {
server.register([
inert,
{
register: understood,
options: {
default: 'en-us', localesDir: path.join(__dirname, '../static/locales')
}
}],
(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(/.*?-.*?/);
}));