Invalid route options ( ) "value" must be an object – Hapi.js/Node.js

Issue

I’m creating a REST-API with Hapi and Node. I’m trying to put my routes in separate files following this guys (How to store routes in separate files when using Hapi?) approach. But with the Code bellow I get this error: Invalid route options ( ) "value" must be an object.

I have a user.js with my routes:

import { UserController } from "../../controllers/user.js";

const baseUrl = '/api/v1';
const userController = new UserController();

export class UserRoutes {

    user() {
        return [
            {
                method: 'GET',
                path: baseUrl + '/users',
                config: {
                    handler: (request, response) => {
                        return userController.find(request, response);
                    }
                }
            },
            {
                method: 'GET',
                path: baseUrl + '/user/{user_id}',
                config: {
                    handler: UserController.findById
                }
            },
        ];
    }
}

and index.js in the same folder, which should merge the other route files (I currently don’t have) into one array of routes:

import { UserRoutes } from './user.js';

export class Routes {
    getRoutes() {
        return [].concat(UserRoutes);
    }
}

and use it in the root index.js like so:

import {Routes} from './src/routes/v1/index.js';

let routes = new Routes();
server.route(routes.getRoutes());

Am I doing something wrong? I also tried CommonJS as in the other Stack Overflow thread I mentioned above – but same error.

Solution

Finnally found what the error was causing. I wanted the Class UserRoutes in return [].concat(UserRoutes); to return the value that the Method inside should return… insert facepalm here. First I had to instaciate the class and then call the method.. as allways…

routes/index.js

    getRoutes() {
        let userRoutes = new UserRoutes();

        return [].concat(userRoutes.user());
    }

Answered By – KastenBrot

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.