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.
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());
}
In my case I got this error because I had a test file for the post route where I forgot to add spec in file name, so make sure your files naming's are correct
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.