Saturday 18 March 2017

how to get pretty urls in hapi js

I have my server.js file working. at localhost:8080 it will serve the file i give it from the the corresponding url name like so http://localhost:8080/about.html, as long as the file exists in public/pages. I'm wondering if I can somehow set a wildcard to leave of extensions for all html files in the url so that I don't have to individually specify each file as an alias in the routes like - ['about','about.html'].

Here is my working code -

'use strict';

const Path = require('path');
const Hapi = require('hapi');

const server = new Hapi.Server();
server.connection({
    port: Number(process.argv[2] || 8080),
    host: 'localhost'
});
server.register(require('inert'), (err) => {

    if (err) {
        throw err;
    }

    server.route({
        method: 'GET',
        path: '/{param*}',
        handler: {
            directory: {
                path: 'public/pages',
                listing: true
            }
        },
        config: {
            state: {
              parse: false, // parse and store in request.state
              failAction: 'ignore' // may also be 'ignore' or 'log'
            }
        }

    });

    server.start((err) => {

        if (err) {
            throw err;
        }

        console.log('Server running at:', server.info.uri);
    });
});

Any help is greatly appreciated, thank you.



via kiko carisse

No comments:

Post a Comment