1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-11 05:40:11 +02:00

Use hapi instead of express

This commit is contained in:
geek 2016-12-30 15:46:55 -06:00
parent 609499bc61
commit f8a0fba192

View File

@ -11,19 +11,26 @@ const index = path.join(__dirname, './index.html');
const html = template(fs.readFileSync(index, 'utf-8')); const html = template(fs.readFileSync(index, 'utf-8'));
const server = new hapi.Server({ const server = new hapi.Server({
connections: { connections: {
routes: { routes: {
files: { files: {
relativeTo: path.join(__dirname, '../static') relativeTo: path.join(__dirname, '../static')
} }
}
} }
}
});
server.connection({
port: process.env.PORT || 8000
}); });
server.connection({ port: process.env.PORT || 8000 });
server.register([ server.register([
inert, inert,
{ register: understood, options: { default: 'en-us', localesDir: path.join(__dirname, '../static/locales') } }], {
register: understood,
options: {
default: 'en-us', localesDir: path.join(__dirname, '../static/locales')
}
}],
(err) => { (err) => {
if (err) { if (err) {
console.error(err); console.error(err);
@ -34,15 +41,17 @@ server.register([
method: 'GET', method: 'GET',
path: '/static/{param*}', path: '/static/{param*}',
handler: { handler: {
directory: { directory: {
path: '.', path: '.',
redirectToSlash: true, redirectToSlash: true,
index: false index: false
} }
} }
}); });
server.route({ method: '*', path: '/{param*}', handler: defaultHandler }); server.route({
method: '*', path: '/{param*}', handler: defaultHandler
});
server.start((err) => { server.start((err) => {
if (err) { if (err) {
@ -52,9 +61,11 @@ server.register([
console.log(`Server running at: http://localhost:${server.info.port}`); console.log(`Server running at: http://localhost:${server.info.port}`);
}); });
}); });
function defaultHandler (request, reply) { function defaultHandler(request, reply) {
const locales = (request.locale || '').toLowerCase().split(/\-/); const locales = (request.locale || '').toLowerCase().split(/\-/);
reply(html({ locale: locales[1], lang: locales[0] })); reply(html({
locale: locales[1], lang: locales[0]
}));
} }