I am using this simple server program
JavaScript
x
18
18
1
const Hapi = require('hapi');
2
const server = new Hapi.Server({
3
host: 'localhost',
4
port: 8080,
5
});
6
7
server.route({
8
path: '/',
9
method: 'GET',
10
handler: (request, response) => {
11
response(true);
12
},
13
});
14
15
server.start(() => {
16
console.log('Server running at:', server.info.uri);
17
});
18
which gave me the following error on server startup
JavaScript
1
21
21
1
throw new Error(msgs.join(' ') || 'Unknown error');
2
^
3
4
Error: Invalid server options {
5
"port" [2]: 8080,
6
"host" [1]: "localhost"
7
}
8
9
[1] "host" is not allowed
10
[2] "port" is not allowed
11
at Object.exports.assert (/Users/aakashverma/Documents/exercises/makeithapi/node_modules/hoek/lib/index.js:736:11)
12
at Object.exports.apply (/Users/aakashverma/Documents/exercises/makeithapi/node_modules/hapi/lib/schema.js:17:10)
13
at new module.exports.internals.Server (/Users/aakashverma/Documents/exercises/makeithapi/node_modules/hapi/lib/server.js:32:22)
14
at Object.<anonymous> (/Users/aakashverma/Documents/exercises/makeithapi/serveEm/serveEm.js:3:16)
15
at Module._compile (module.js:660:30)
16
at Object.Module._extensions..js (module.js:671:10)
17
at Module.load (module.js:573:32)
18
at tryModuleLoad (module.js:513:12)
19
at Function.Module._load (module.js:505:3)
20
at Function.Module.runMain (module.js:701:10)
21
and my package.json
has dependencies set this way
JavaScript
1
5
1
"dependencies": {
2
"axios": "^0.17.1",
3
"hapi": "^16.6.2"
4
}
5
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?
Advertisement
Answer
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:
JavaScript
1
15
15
1
'use strict';
2
3
const Hapi = require('hapi');
4
5
const server = new Hapi.Server();
6
server.connection({ port: 3000, host: 'localhost' });
7
8
server.start((err) => {
9
10
if (err) {
11
throw err;
12
}
13
console.log(`Server running at: ${server.info.uri}`);
14
});
15