has anyone run into a problem where Hapi intermittently throws CORS access errors?
we have an old backend server (Hapi 14.x) running on AWS and a frontend Angular service with a different URL. when the frontend queries the server sometimes it works just fine but then at other times it randomly errors out with CORS problems and i haven't been able to determine a pattern.
the server side has the following code on initialization
cors: {
credentials: true
}
reading the docs it seems like without explicitly setting origin
it defaults to ['*']
var Hapi=require('hapi');
var routes=require('./Routes/routes');
var admin=require('./Model/admin');
admin.createAdmin();
var port=process.env.port||3000;
var server = new Hapi.Server()
server.connection({
port:port,
host:'localhost',
routes: {
cors: true
}
})
routes.forEach((route, index)=>{
server.route(route)
})
server.register(
[
{
register:require('good'),
options:{
ops:{
interval:10000
},
reporters:{
console:[
{
module:'good-squeeze',
name:'Squeeze',
args:[{log:'*',response:'*',request:'*'}]
},
{
module:'good-console'
},
'stdout'
]
}
}
},
],
function(err){
if(err) {console.log('error',"failed to install plugins")
throw err;
}
server.log("info","plugins registered")
}
);
server.start( function(err){
if(err) throw err;
console.log('server running at '+server.info.uri );
}
Hi! I'm not sure if it is a bug
I'm using "@hapi/hapi": "^18.3.2"
, "@hapi/joi": "^16.0.1"
,
My routes looks like:
'use strict';
const Joi = require('@hapi/joi');
module.exports = [
{
method: 'GET',
path: '/users/new',
handler: (request, h) => {
return h.view('users/new');
}
},
{
method: 'POST',
path: '/users',
handler: (request, h) => {
return h.view('users/new');
},
options: {
validate: {
payload: Joi.object().keys({
username: Joi.string()
})
}
}
}
];
I get the following error when I start the server:
Error: Invalid schema content: ($_root.alternatives)
at new module.exports (/home/sitraka/Projects/camoicenter/node_modules/@hapi/hoek/lib/error.js:21:15)
at Object.module.exports [as assert] (/home/sitraka/Projects/camoicenter/node_modules/@hapi/hoek/lib/assert.js:20:11)
at Object.exports.schema (/home/sitraka/Projects/camoicenter/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/cast.js:50:10)
at internals.Object.keys (/home/sitraka/Projects/camoicenter/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/types/object/index.js:363:35)
at Object.exports.schema (/home/sitraka/Projects/camoicenter/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/cast.js:31:29)
at internals.Object.keys (/home/sitraka/Projects/camoicenter/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/types/object/index.js:363:35)
at Object.exports.schema (/home/sitraka/Projects/camoicenter/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/cast.js:31:29)
at module.exports.internals.Any.root.compile (/home/sitraka/Projects/camoicenter/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/index.js:157:25)
at Object.exports.compile (/home/sitraka/Projects/camoicenter/node_modules/@hapi/hapi/lib/validation.js:21:49)
at forEach (/home/sitraka/Projects/camoicenter/node_modules/@hapi/hapi/lib/route.js:196:43) path: '$_root.alternatives' }
im having hard time finding good documentation on how to setup ssl
this is what I have
const server = hapi.server({
port: port,
host: configs.APIServer[env].Hostname,
routes: {
cors: {
origin: [configs.APIServer[env].Protocol +
"://" +
configs.WebServer[env].Hostname +
":" +
configs.WebServer[env].Port, configs.WebServer[env].Protocol +
"://" +
configs.WebServer[env].IP +
":" +
configs.WebServer[env].Port
]
}
}
// ,debug: {
// request: env === "production" ? ["error"] : ["error", "debug"]
// }
})
do i just need to add on the bottom:
, {
tls: ssloptions
}
where ssloptions is the certs
-I, --ignore - ignore a list of globals for the leak detection (comma separated), this is an alias of globals property in .labrc file. To ignore symbols, pass the symbol's string representation (e.g. Symbol(special)).
initServer(server: Server)
where Server
is imported from @hapi/hapi.Server
. When I pass to this function a variable named server: Hapi.Server = new Hapi.Server()
where Hapi
is imported from @types/hapi
, the compiler yields the error Property 'token' is missing in type 'import("/home/foo/bar/myproject/node_modules/@types/hapi/index").AuthCredentials' but required in type 'import("/home/foo/bar/myproject/node_modules/@types/hapi__hapi/index").AuthCredentials'
. Why are the two types different? How can I solve this problem? Thanks :)
@types/hapi
is for package the package hapi
, and the official package moved to @hapi/hapi
this year, and you need to use @types/hapi__hapi