Skip to content

Instantly share code, notes, and snippets.

@csakis
Created January 16, 2018 18:40
Show Gist options
  • Save csakis/93178a0bbb25e4c26968d5cbd5957da5 to your computer and use it in GitHub Desktop.
Save csakis/93178a0bbb25e4c26968d5cbd5957da5 to your computer and use it in GitHub Desktop.
Basic hapi application with handlebars templating and Inert for static content
const Path = require('path');
const Hapi = require('hapi');
const Inert = require('inert');
const Vision = require('vision');
const Handlebars = require('handlebars');
const server = Hapi.server({
port: 3000,
routes: {
files: {
relativeTo: Path.join(__dirname, 'public')
}
}
});
const start = async () => {
await server.register(Inert);
await server.register(Vision);
server.views({
engines: {
hbs: Handlebars },
relativeTo: Path.join(__dirname, 'templates'),
isCached: false,
partialsPath: 'partials',
helpersPath: 'helpers',
layout:true
});
server.route({
method: 'get',
path: '/',
handler: (request, h) => {
return h.view('index', {title: "Index"})
}
});
server.route({
method: 'GET',
path: '/login',
handler: (request, h) => {
return h.view('login')
}
});
server.route({
method: 'POST',
path: '/login',
handler: (request, h) => {
return h.view('login')
}
});
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: '.',
redirectToSlash: true,
index: true
}
}
});
await server.start();
console.log("Hapi is running on port", server.info.uri);
};
start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment