Easily customisable template for a Node server using hapi with handlebars.js templating.
- Clone the repo
- Run
npm i
to install dependencies and dev dependencies. - Run
npm start
to start the server - Visit
localhost:4000
on your browser to view the website - woo!
- Where Stuff Is:
- The server already has a home route and a resource handler.
- The default HTML for all pages is specified in
views/layout/default.html
and includes the navbar and footer which are inviews/partials
. - Minimal CSS, an empty script.js file, and a folder for assets are in
public
. - A test file with three passing tests is in
tests
- To add new routes to the server:
- Make a new js file in the
routes
folder, e.g.blog.js
. This should be in the same form as thehome.js
andresources.js
files, i.e. an exported object including a method, path, and handler function:
module.exports = {
method: 'GET',
path: '/blog',
handler: (request, reply) => {
reply.view('blog');
}
};
- Add the new route to ther server by adding its file path to the list of routes at line 26, e.g.
server.route([
'./routes/home.js',
'./routes/resources.js',
'./routes/blog.js'
].map((route) => require(route)));
- If your route is a new view, you can add the HTML for that page in the
views
folder, e.g. for our blog page, we would make a file calledblog.html
. - Hapi has lots of helpful plugins for other things you might want, like authentication and validation. Check out their list here.
- Please make a PR if you think something could be clearer, cleaner, or better.