2016-02-29 28 views

Odpowiedz

3

Dobrym sposobem na osiągnięcie tego przy pomocy hapi jest umieszczenie różnych witryn w celu oddzielenia plugins i użycia modyfikatora vhost podczas ładowania wtyczki, najlepiej przy użyciu Glue.

Oto przykład:

sites/dogs.js

exports.register = function (server, options, next) { 

    // Put all your routes for the site in here 

    server.route({ 
     method: 'GET', 
     path: '/', 
     handler: function (request, reply) { 

      reply('Dogs homepage'); 
     } 
    }); 

    next(); 
}; 

exports.register.attributes = { name: 'dogs' }; 

sites/cats.js

exports.register = function (server, options, next) { 

    // Put all your routes for the site in here 

    server.route({ 
     method: 'GET', 
     path: '/', 
     handler: function (request, reply) { 

      reply('Cats homepage'); 
     } 
    }); 

    next(); 
}; 

exports.register.attributes = { name: 'cats' }; 

index.js

const Glue = require('glue'); 
const Hoek = require('hoek'); 

const manifest = { 
    connections: [{ 
     port: 4000, 
    }], 
    registrations: [ 
     { 
      plugin: { 
       register: './sites/cats' 
      }, 
      options: { 
       routes: { 
        vhost: 'cats.com' 
       } 
      } 
     }, 
     { 
      plugin: { 
       register: './sites/dogs' 
      }, 
      options: { 
       routes: { 
        vhost: 'dogs.com' 
       } 
      } 
     } 
    ] 
}; 

const options = { 
    relativeTo: __dirname 
}; 

Glue.compose(manifest, options, (err, server) => { 

    Hoek.assert(!err, err); 
    server.start((err) => { 

     Hoek.assert(!err, err); 
     console.log('server started'); 
    }); 
}); 

Następnie można potwierdzić, że routing działa poprawnie z kilkoma cURL poleceń:

$ curl -H "Host: cats.com" localhost:4000/ 
Cats homepage 

$ curl -H "Host: dogs.com" localhost:4000/ 
Dogs homepage 

Przeglądarka ustawi ten nagłówek hosta dla Ciebie choć tak podczas przeglądania http://cats.com lub http://dogs.com Hapi będzie służył poprawna treść (pod warunkiem, że twój DNS jest poprawnie skonfigurowany).

+0

Jak mogę wyświetlać szablony? – eosimosu