问题
I am trying to host api and web servers on the same node code stack. I have used labels in order to apply configurations independently to each server but only one server works. Below is the code:
var hapi = require('hapi');
// server definition
var server = new hapi.Server();
var runningPort = process.env.PORT || 3000;
// setting up connection
server.connection({
host: '0.0.0.0',
port: runningPort,
labels: ['api']
});
server.connection({
host: '0.0.0.0',
port: runningPort,
labels: ['web']
});
var webServer = server.select('web');
var apiServer = server.select('api');
// registering view engine
webServer.views({
engines: { html: require('handlebars') },
relativeTo: __dirname,
path: './views',
layoutPath: './views/layout',
layout: 'default',
partialsPath: './views/partials'
});
// registering hapi auth cookie and application authentication
webServer.register(
{
register: require('hapi-auth-cookie')
},
function (err) {
if (err) {
throw err;
}
var cache = webServer.cache({ segment: 'sessions', expiresIn: 3 * 24 * 60 * 60 * 1000 });
webServer.app.cache = cache;
webServer.auth.strategy('session', 'cookie', true, {
password: 'secret',
cookie: 'sid-example',
redirectTo: '/account/login',
isSecure: false
});
});
// registrations for api server
apiServer.register(
{
register: require('lout')
},
function (err) {
if (err) {
throw err;
}
});
apiServer.register(require('hapi-auth-bearer-token'), function (err) {
apiServer.auth.strategy('simple', 'bearer-access-token', {
allowQueryToken: true, // optional, true by default
allowMultipleHeaders: false, // optional, false by default
accessTokenName: 'access_token', // optional, 'access_token' by default
validateFunc: function( token, callback ) {
// For convenience, the request object can be accessed
// from `this` within validateFunc.
var request = this;
// Use a real strategy here,
// comparing with a token from your database for example
if(token === "1234"){
//## user object to be looked up here
callback(null, true, { token: token })
} else {
callback(null, false, { token: token })
}
}
});
});
//To do something to request before they passed on to routes
apiServer.ext('onRequest', function (request, reply) {
//## we can get user object here off of the authToken
utils.log('info', 'apiCall', {method: request.method, path: request.path})
return reply.continue();
});
// register routes
webServer.route(webRoutes);
apiServer.route(apiRoutes);
server.start(function () {
console.log('Web servers running at: ', 'localhost:' + runningPort);
console.log('Api server running at: ', 'localhost:' + runningPort);
});
Currently only api routes work.
回答1:
As mentioned by the commenters, you can't create 2 connections to the same port on the same network interface. This goes all the way back to the listen syscall giving a EADDRINUSE error if two sockets try to listen on the same port.
Creating two connections on separate ports or separate network interfaces is perfectly ok though:
server.connection({
host: '127.0.0.1',
port: 4000,
labels: ['api']
});
server.connection({
host: '127.0.0.1',
port: 4001,
labels: ['web']
});
回答2:
As Matt already said, you can't run different connections on the same port. Make sure to apply different ports, like
server.connection({
host: 'localhost',
port: process.env.PORT || port
});
server.connection({
host: 'localhost',
port: process.env.PORT + 1 || port + 1
});
If you want to read more on running an API and your web server within the same hapi project, you might scan through those tutorials:
- https://futurestud.io/tutorials/hapi-how-to-run-separate-frontend-and-backend-servers-within-one-project
- https://futurestud.io/tutorials/hapi-how-to-use-server-labels
Hope that helps!
来源:https://stackoverflow.com/questions/31569245/how-to-run-multiple-server-labels-on-same-port-using-node-and-hapi