Reputation: 3994
I am using this simple server program
const Hapi = require('hapi');
const server = new Hapi.Server({
host: 'localhost',
port: 8080,
});
server.route({
path: '/',
method: 'GET',
handler: (request, response) => {
response(true);
},
});
server.start(() => {
console.log('Server running at:', server.info.uri);
});
which gave me the following error on server startup
throw new Error(msgs.join(' ') || 'Unknown error');
^
Error: Invalid server options {
"port" [2]: 8080,
"host" [1]: "localhost"
}
[1] "host" is not allowed
[2] "port" is not allowed
at Object.exports.assert (/Users/aakashverma/Documents/exercises/makeithapi/node_modules/hoek/lib/index.js:736:11)
at Object.exports.apply (/Users/aakashverma/Documents/exercises/makeithapi/node_modules/hapi/lib/schema.js:17:10)
at new module.exports.internals.Server (/Users/aakashverma/Documents/exercises/makeithapi/node_modules/hapi/lib/server.js:32:22)
at Object.<anonymous> (/Users/aakashverma/Documents/exercises/makeithapi/serveEm/serveEm.js:3:16)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
and my package.json
has dependencies set this way
"dependencies": {
"axios": "^0.17.1",
"hapi": "^16.6.2"
}
I tried searching for this issue everywhere and found an exact one here but the versions are too old to be compared.
How do I resolve this problem?
Upvotes: 1
Views: 1428
Reputation: 5738
The options you're passing need to be passed to a call to server.connection()
rather than into the Server
constructor.
Snippet from hapi docs:
'use strict';
const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({ port: 3000, host: 'localhost' });
server.start((err) => {
if (err) {
throw err;
}
console.log(`Server running at: ${server.info.uri}`);
});
Upvotes: 4
Reputation: 622
Could it be typo issue ? Because in their sample code here they spelled server with lowercase, and you create a new instance while the sample is just using the function.
// Create a server with a host and port
const server = Hapi.server({
host: 'localhost',
port: 8000
});
Update:
Apparently, the current version for hapi is 17, and the question is asked for 16. So, the details of how to configure a server in version 16.6.2 can be found below:
Upvotes: 1