Created
January 16, 2018 18:40
-
-
Save csakis/93178a0bbb25e4c26968d5cbd5957da5 to your computer and use it in GitHub Desktop.
Basic hapi application with handlebars templating and Inert for static content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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