js community,
I'm encountering a peculiar issue with my Hapi.js server where I'm consistently seeing "Debug: request, error, close" messages in the logs. However, the GET method appears to be running well without errors.
Here's a simplified overview of my server setup:
"dependencies": { "@hapi/boom": "^8.0.1", "@hapi/hapi": "^18.4.0", "@hapi/inert": "^5.2.2", "@hapi/joi": "^16.1.7", "@hapi/vision": "^5.5.4", "axios": "^0.19.0", "dotenv": "^8.2.0", "form-data": "^4.0.0", "hapi-auth-jwt2": "^8.8.0", "hapi-pino": "^6.3.0", "hapi-swagger": "^11.0.0", "jsonwebtoken": "^8.5.1", "lodash": "^4.17.15", "mongoose": "^5.7.11", "p-map": "^4.0.0", "p-settle": "4.1.1", "typedi": "^0.8.0" }, "devDependencies": { "nodemon": "^1.19.4" }
this is my server.js
const Hapi = require("@hapi/hapi");
const hapiJwt = require("hapi-auth-jwt2");
const Inert = require("@hapi/inert");
const Vision = require("@hapi/vision");
const HapiSwagger = require("hapi-swagger");
const HapiPino = require("hapi-pino");
const authRoutes = require("./api/authRoutes");
const userRoutes = require("./api/userRoutes");
const otpRoutes = require("./api/otpRoutes");
const webRoutes = require("./api/webRoutes");
const config = require("./config");
const { UserModel } = require("./models/user");
const logger = require("./helpers/logger");
const fs = require("fs");
const path = require("path");
const validate = async function (decoded, request, h) {
const User = await UserModel();
const user = await User.findOne({
sessionId: decoded.sessionId,
_id: decoded.userId,
});
if (user == null) {
logger.error("validate request = user not found!");
return { isValid: false, errorMessage: "ERR_USER_NOT_FOUND" };
}
if (!user.enabled) {
logger.error("validate request = user disabled!!");
return { isValid: false, errorMessage: "ERR_USER_DISABLED" };
}
logger.debug("token validated");
return { isValid: true };
};
const init = async () => {
const server = new Hapi.Server({
port: config.port || 8000,
host: "0.0.0.0",
debug: { request: ["*"] },
});
// server.ext({
// type: "onPostAuth",
// method: function(request, h) {
// if (request.auth.credentials) {
// logger.info("onPostAuth", request.auth.credentials.userId);
// }
// return h.continue;
// }
// });
server.ext({
type: "onPreAuth",
method: function (request, h) {
logger.info("Request => " + request.url);
return h.continue;
},
});
server.ext({
type: "onPreResponse",
method: function (request, h) {
const response = request.response;
if (!response.isBoom) {
logger.info("Response => OK");
return h.continue;
}
const code = (response.data && response.data.err_code) || "ERR_UNKNOWN";
response.output.payload.errorCode = code;
response.reformat();
// logger.error("Response Error => %s", code);
return response;
},
});
const swaggerOptions = {
info: {
title: "test",
version: "1.1.0",
},
};
// register plugins
await server.register([
hapiJwt,
Inert,
Vision,
{ plugin: HapiSwagger, options: swaggerOptions },
// {
// plugin: HapiPino,
// options: {
// instance: logger,
// logPayload: false
// }
// }
]);
// init jwt
await server.auth.strategy("jwt", "jwt", {
key: config.jwtSecretKey,
validate: validate,
verifyOptions: { algorithms: ["HS256"], tokenType: "bearer" },
});
server.auth.default("jwt");
server.route(authRoutes);
server.route(userRoutes);
server.route(otpRoutes);
server.route(webRoutes);
await server.start();
logger.info("Server running on %s", server.info.uri);
};
process.on("unhandledRejection", (err) => {
logger.info(err);
process.exit(1);
});
init();
Note:
- Handler not executed
- Validate not executed
- Only onPreAuth executed The GET method works as expected, but other methods encounter errors. Server initialization appears to be successful.
I'm seeking guidance on how to troubleshoot and resolve this issue. Any insights, suggestions, or recommendations on how to approach this problem would be greatly appreciated.
Thank you for your time and assistance!
I am expecting to use another Method, this server only running well if i change POST method with GET