Related
im trying to build a dynamic module for dynamically insert additional endpoints to certain routes modules (adding a generic crud endpoints):
crud.module.ts
#Module({})
export class CrudModule {
static register(config): DynamicModule {
const controller = getController(config.route);
return {
module: TestModule,
controllers: [controller],
};
}
}
getController.ts
function getController(route: string): Type<any> {
#ApiTags(`${route}`)
#Controller(`${route}/crud`)
class CrudController {
#Get()
get() {
return route;
}
}
return CrudController;
}
export { getController };
And adding to specified routes modules
route1.module.ts
#Module({
imports: [
CrudModule.register({
route: 'route1',
}),
],
providers: [UserService],
controllers: [UserController],
})
export class Route1Module {}
route2.module.ts
#Module({
imports: [
CrudModule.register({
route: 'route2',
}),
],
providers: [UserService],
controllers: [UserController],
})
export class Route2Module {}
routes.module.ts
#Module({
imports: [Route1Module, Route2Module],
})
export class RoutesModule {}
The problem is that the routes generated from CrudModule only is being mapped for the first imported route module (in this case, just for Route1Module), I need a bit help here for get mapping the routes from crud for every Module where is being registered
================ EXTRA INFO
Adding some logs about is loading the CrudModule in the different route modules
#Module({})
export class CrudModule {
static register(config): DynamicModule {
console.log({ route: config.route })
const controller = getController(config.route);
return {
module: TestModule,
controllers: [controller],
};
}
}
[6:37:40 PM] Found 0 errors. Watching for file changes.
**{ route: 'route1' }
{ route: 'route2' }**
[Nest] 90418 - 11/13/2022, 6:37:40 PM LOG [NestFactory] Starting Nest application...
[Nest] 90418 - 11/13/2022, 6:37:40 PM LOG [InstanceLoader] AppModule dependencies initialized +21ms
[Nest] 90418 - 11/13/2022, 6:37:40 PM LOG [InstanceLoader] RoutesModule dependencies initialized +0ms
*[Nest] 90418 - 11/13/2022, 6:37:40 PM LOG [InstanceLoader] Route1Module dependencies initialized +0ms
[Nest] 90418 - 11/13/2022, 6:37:40 PM* LOG [InstanceLoader] Route2Module dependencies initialized +0ms
[Nest] 90418 - 11/13/2022, 6:37:40 PM LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 90418 - 11/13/2022, 6:37:40 PM LOG [InstanceLoader] CacheModule dependencies initialized +0ms
**[Nest] 90418 - 11/13/2022, 6:37:40 PM LOG [InstanceLoader] CrudModule dependencies initialized +0ms**
[Nest] 90418 - 11/13/2022, 6:37:40 PM LOG [InstanceLoader] InterceptorsModule dependencies initialized +0ms
**[Nest] 90418 - 11/13/2022, 6:37:40 PM LOG [RoutesResolver] CrudController {/route1/crud}: +225ms
[Nest] 90418 - 11/13/2022, 6:37:40 PM LOG [RouterExplorer] Mapped {/route1/crud, GET} route +1ms**
[Nest] 90418 - 11/13/2022, 6:37:40 PM LOG [NestApplication] Nest application successfully started +0ms
[Nest] 90418 - 11/13/2022, 6:37:40 PM LOG [Application is running on: http://localhost:3000]
The log trigger both module loaded, but only once instance of CrudModule is initialised
============= SOLUTION
Thinking about the dependencies initialised and the overriding between controllers/modules naming.
I testing about to dynamically change the name of controller loaded by each module where is being used
#Module({})
export class CrudModule {
static registre(route: string): DynamicModule {
const Controller = getController(route);
Object.defineProperty(Controller, 'name', { value: `${route}Controller` });
console.log(Controller);
return {
module: CrudModule,
controllers: [Controller],
providers: [Controller],
exports: [Controller],
};
}
}
I got finally both dynamic controllers being initialised and working correctly
[class route1Controller]
[class route2Controller]
[Nest] 90617 - 11/13/2022, 6:51:28 PM LOG [NestFactory] Starting Nest application...
[Nest] 90617 - 11/13/2022, 6:51:28 PM LOG [InstanceLoader] AppModule dependencies initialized +21ms
[Nest] 90617 - 11/13/2022, 6:51:28 PM LOG [InstanceLoader] RoutesModule dependencies initialized +0ms
[Nest] 90617 - 11/13/2022, 6:51:28 PM LOG [InstanceLoader] Route1Module dependencies initialized +0ms
[Nest] 90617 - 11/13/2022, 6:51:28 PM LOG [InstanceLoader] Route2Module dependencies initialized +0ms
[Nest] 90617 - 11/13/2022, 6:51:28 PM LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 90617 - 11/13/2022, 6:51:28 PM LOG [InstanceLoader] CacheModule dependencies initialized +0ms
[Nest] 90617 - 11/13/2022, 6:51:28 PM LOG [InstanceLoader] CrudModule dependencies initialized +0ms
[Nest] 90617 - 11/13/2022, 6:51:28 PM LOG [InstanceLoader] CrudModule dependencies initialized +0ms
[Nest] 90617 - 11/13/2022, 6:51:28 PM LOG [InstanceLoader] InterceptorsModule dependencies initialized +1ms
[Nest] 90617 - 11/13/2022, 6:51:28 PM LOG [RoutesResolver] route1Controller {/route1/crud}: +225ms
[Nest] 90617 - 11/13/2022, 6:51:28 PM LOG [RouterExplorer] Mapped {/route1/crud, GET} route +1ms
[Nest] 90617 - 11/13/2022, 6:51:28 PM LOG [RoutesResolver] route2Controller {/route2/crud}: +0ms
[Nest] 90617 - 11/13/2022, 6:51:28 PM LOG [RouterExplorer] Mapped {/route2/crud, GET} route +0ms
[Nest] 90617 - 11/13/2022, 6:51:28 PM LOG [NestApplication] Nest application successfully started +1ms
[Nest] 90617 - 11/13/2022, 6:51:28 PM LOG [Application is running on: http://localhost:3000]
``
I have a nestjs module called AuthModule
import { Module } from '#nestjs/common';
import { AuthService } from './auth.service';
import { TypeOrmModule } from '#nestjs/typeorm';
import { UsersEntity } from './user-auth-credentials.entity';
import { UsersRepository } from './users.repository';
import { AuthController } from './auth.controller';
#Module({
imports: [TypeOrmModule.forFeature([UsersRepository])],
controllers: [AuthController],
providers: [AuthService],
})
export class AuthModule {}
a controller called **AuthController**.
import { Controller, Get } from '#nestjs/common';
import { AuthService } from './auth.service';
#Controller('auth')
export class AuthController {
constructor(private authService: AuthService) {}
#Get()
findAll() {
return 'Hello World';
}
}
The methods in the controller findAll as an example are not being mapped during compiling as shown by the output of npm run start:dev below.
[12:36:39] File change detected. Starting incremental compilation...
[12:36:39] Found 0 errors. Watching for file changes.
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [NestFactory] Starting Nest application...
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [InstanceLoader] TypeOrmModule dependencies initialized +122ms
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [InstanceLoader] AppModule dependencies initialized +1ms
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [InstanceLoader] TypeOrmCoreModule dependencies initialized +73ms
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [InstanceLoader] AuthModule dependencies initialized +0ms
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [InstanceLoader] TeachersModule dependencies initialized +1ms
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [RoutesResolver] AppController {/}: +7ms
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [RouterExplorer] Mapped {/, GET} route +4ms
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [RoutesResolver] TeachersController {/teachers}: +0ms
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [RouterExplorer] Mapped {/teachers, GET} route +1ms
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [RouterExplorer] Mapped {/teachers/id, GET} route +2ms
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [RouterExplorer] Mapped {/teachers, POST} route +1ms
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [RouterExplorer] Mapped {/teachers/id, PATCH} route +1ms
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [RouterExplorer] Mapped {/teachers/id, DELETE} route +1ms
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [RoutesResolver] AuthController {/auth}: +0ms
[Nest] 4262 - 09/03/2022, 12:36:40 LOG [NestApplication] Nest application successfully started +4ms
When I access the route using Insomnia i get this
output of Insomnia
Is there something Im not doing right?
your code looks fine but the output didn't show any Mapped {/auth, GET}. I'd say that you're probably running an old version of the your code (which doesn't have that findAll method)
Try rm -rf dist and start it again.
I'am trying to deploy my NestJs application on an EC2 instance.
I cloned my project and installed all dependencies but then when I tried to start the app, it can't initialize all dependencies (can't even reach TypeOrmCoreModule dependencies).
I have no errors.
[Nest] 117257 - 09/27/2021, 1:14 AM [NestFactory] Starting Nest application...
[Nest] 117257 - 09/27/2021, 1:14 AM [InstanceLoader] TypeOrmModule dependencies initialized +176ms
[Nest] 117257 - 09/27/2021, 1:14 AM [InstanceLoader] PassportModule dependencies initialized +0ms
[Nest] 117257 - 09/27/2021, 1:14 AM [InstanceLoader] NeconfigModule dependencies initialized +1ms
[Nest] 117257 - 09/27/2021, 1:14 AM [InstanceLoader] JwtModule dependencies initialized +0ms
[Nest] 117257 - 09/27/2021, 1:14 AM [InstanceLoader] CommentsModule dependencies initialized +0ms
[Nest] 117257 - 09/27/2021, 1:14 AM [InstanceLoader] MsPostsModule dependencies initialized +1ms
[Nest] 117257 - 09/27/2021, 1:14 AM [InstanceLoader] PostCommentsModule dependencies initialized +0ms
[Nest] 117257 - 09/27/2021, 1:14 AM [InstanceLoader] ConfigHostModule dependencies initialized +1ms
[Nest] 117257 - 09/27/2021, 1:14 AM [InstanceLoader] ConfigModule dependencies initialized +11ms
[Nest] 117257 - 09/27/2021, 1:14 AM [InstanceLoader] AppModule dependencies initialized +1ms
I my local machine the Nest application is successfully started.
[Nest] 15660 - 27/09/2021, 03:10:58 [NestFactory] Starting Nest application...
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] TypeOrmModule dependencies initialized +112ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] PassportModule dependencies initialized +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] NeconfigModule dependencies initialized +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] JwtModule dependencies initialized +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] CommentsModule dependencies initialized +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] MsPostsModule dependencies initialized +0ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] PostCommentsModule dependencies initialized +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] ConfigHostModule dependencies initialized +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] ConfigModule dependencies initialized +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] AppModule dependencies initialized +0ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] TypeOrmCoreModule dependencies initialized +236ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] NotificationModule dependencies initialized +16ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] PostContentModule dependencies initialized +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] FollowersTsModule dependencies initialized +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] FavorisModule dependencies initialized +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] PostModule dependencies initialized +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] AccountModule dependencies initialized +0ms
[Nest] 15660 - 27/09/2021, 03:10:58 [InstanceLoader] UsersModule dependencies initialized +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RoutesResolver] AppController {/}: +8ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/facebook, GET} route +3ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/facebook/redirect, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/authorize_user, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/handleauth, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/handleauthcode, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/google, GET} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/google/redirect, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RoutesResolver] UsersController {/users}: +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/createUser, POST} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/:id, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/search/:email, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/:id, PUT} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/:id, DELETE} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/login, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/confirm/:id, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/resetPassword, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/changePassword/:id, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/follow, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/unfollow, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/inst, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/googlelogin, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/facebooklogin, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/testTypeCnx/:id, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RoutesResolver] NotificationController {/notification}: +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/addNotif, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getNotif, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RoutesResolver] CommentsController {/comments}: +0ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RoutesResolver] MsPostsController {/ms-posts}: +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RoutesResolver] AccountController {/account}: +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/createOneAccount, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getAllUser, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getUserByName/:userName, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/UpdateUser/:userName, PUT} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/DeleteByUsername/:userName, DELETE} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/addFromxlsx, POST} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/showAllUsername, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getBestAccountLastMonth, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getBestAccountLastYear, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getRankAccountLastWeek, GET} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getBestAccountByDay, GET} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getRank/:starName/:dateTest, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getFollowersByDay/:userName, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getFollowersByMonth/:starName, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getFollowersByweek/:starName, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/compare/:starName1/:starName2, GET} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/createData/:starName/:fieldScrape/:countryScrape, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RoutesResolver] PostController {/post}: +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/allPosts, GET} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/country, GET} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/username/:userName/:skip, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/Rankingpermonth/:month/:year/:skip, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/:userName/Rankingpermonth/:month/:year/:skip, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/maxMonth, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/avgLikesMonth, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/avgCommentMonth, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/RankingLastweek/:skip, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/:userName/RankingLastweek/:skip, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/Rankingperyear/:year/:skip, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/:userName/Rankingperyear/:year/:skip, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/totalYear, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/maxYear, GET} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/likesYear/:year/:skip, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/likesMonth/:year/:month/:skip, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/likesAllTimes/:skip, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/celeblikesAllTimes/:userName/:skip, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/celeblikesByMonth/:userName/:year/:month, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/celeblikesByYear/:userName/:year/:skip, GET} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/avgCommentYear, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/maxCommentYear, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RoutesResolver] PostContentController {/postContent}: +0ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/postId, GET} route +3ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RoutesResolver] PostCommentsController {/post-comments}: +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RoutesResolver] FavorisController {/favoris}: +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/addFavoris/:idUser, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getAllFavoris/:idUser, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/addFavorisToAccount/:idUser/:starName, GET} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/checkFavorit/:idUser/:starName, GET} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/FavorisByYear/:idUser, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/FavorisByMonth/:idUser, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/FavorisByDay/:idUser, GET} route +3ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/fovorisAll/:idUser, GET} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/deleteFavoris/:idUser/:starName, DELETE} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/FavorisByWeek/:idUser, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RoutesResolver] FollowersTsController {/followers-ts}: +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/insertDataToDataBase/:username, POST} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getAllData/:starName, GET} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getDataofSpecificDate/:starName, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getAccountLastYear, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getAccountLastMonth, GET} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/getAccountLastWeek, GET} route +2ms
[Nest] 15660 - 27/09/2021, 03:10:58 [RouterExplorer] Mapped {/scrappingInsta/:starName/:fieldScrape/:countryScrape, POST} route +1ms
[Nest] 15660 - 27/09/2021, 03:10:58 [NestApplication] Nest application successfully started +9ms
And this is my main.ts file
import { NestFactory } from '#nestjs/core';
import { AppModule } from './app.module';
import * as dotenv from "dotenv";
import * as http from 'http';
import * as express from 'express';
const fs = require('fs');
dotenv.config();
async function bootstrap() {
const server = express();
const app = await NestFactory.create(
AppModule,
);
app.enableCors({
"origin": "*",
"methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"preflightContinue": false,
"allowedHeaders" : ['Content-Type']
});
await app.init();
http.createServer(server).listen(3005);
}
bootstrap();
I can't figure out why the app can't be started successfully in the ec2 instance.
I don't see any status or console messages in the IDE console while trying to hit any end-point. I do see the response getting returned but no call details in the console. I am confused as why is it behaving this way?
Following is what I see on running npm run-script start:dev -
> nest start --watch
[3:04:44 PM] Starting compilation in watch mode...
[Nest] 14340 - 05/20/2020, 3:10:58 PM [NestFactory] Starting Nest application...
[Nest] 14340 - 05/20/2020, 3:10:58 PM [InstanceLoader] TypeOrmModule dependencies initialized +220ms
[Nest] 14340 - 05/20/2020, 3:10:58 PM [InstanceLoader] TypeOrmCoreModule dependencies initialized +195ms
[Nest] 14340 - 05/20/2020, 3:10:58 PM [InstanceLoader] TypeOrmModule dependencies initialized +6ms
[Nest] 14340 - 05/20/2020, 3:10:58 PM [InstanceLoader] AppModule dependencies initialized +5ms
[Nest] 14340 - 05/20/2020, 3:10:58 PM [RoutesResolver] AppController {}: +13ms
[Nest] 14340 - 05/20/2020, 3:10:58 PM [RouterExplorer] Mapped {, GET} route +4ms
[Nest] 14340 - 05/20/2020, 3:10:58 PM [RoutesResolver] BatchController {/batch}: +1ms
[Nest] 14340 - 05/20/2020, 3:10:58 PM [RouterExplorer] Mapped {/batch, POST} route +1ms
[Nest] 14340 - 05/20/2020, 3:10:58 PM [RouterExplorer] Mapped {/batch, GET} route +1ms
[Nest] 14340 - 05/20/2020, 3:10:58 PM [RouterExplorer] Mapped {/batch/:batch, GET} route +1ms
[Nest] 14340 - 05/20/2020, 3:10:58 PM [RouterExplorer] Mapped {/batch/:batch, DELETE} route +1ms
[Nest] 14340 - 05/20/2020, 3:10:58 PM [RoutesResolver] StudentController {/student}: +0ms
[Nest] 14340 - 05/20/2020, 3:10:58 PM [RouterExplorer] Mapped {/student, POST} route +2ms
[Nest] 14340 - 05/20/2020, 3:10:58 PM [RouterExplorer] Mapped {/student, GET} route +1ms
[Nest] 14340 - 05/20/2020, 3:10:58 PM [RouterExplorer] Mapped {/student/:id, GET} route +1ms
[Nest] 14340 - 05/20/2020, 3:10:58 PM [RouterExplorer] Mapped {/student/:id, DELETE} route +2ms
[Nest] 14340 - 05/20/2020, 3:10:59 PM [RoutesResolver] AssignmentController {/assignment}: +1ms
[Nest] 14340 - 05/20/2020, 3:10:59 PM [RouterExplorer] Mapped {/assignment, POST} route +1ms
[Nest] 14340 - 05/20/2020, 3:10:59 PM [RouterExplorer] Mapped {/assignment, GET} route +1ms
[Nest] 14340 - 05/20/2020, 3:10:59 PM [RouterExplorer] Mapped {/assignment/:id, GET} route +2ms
[Nest] 14340 - 05/20/2020, 3:10:59 PM [RouterExplorer] Mapped {/assignment/:id, DELETE} route +1ms
[Nest] 14340 - 05/20/2020, 3:10:59 PM [RoutesResolver] UploadController {/upload}: +1ms
[Nest] 14340 - 05/20/2020, 3:10:59 PM [RouterExplorer] Mapped {/upload, POST} route +2ms
[Nest] 14340 - 05/20/2020, 3:10:59 PM [NestApplication] Nest application successfully started +3ms
When I hit http://localhost:3000/batch, I see the response but the console above doesn't display anything like API type GET or status 200 etc.
This is what I have in controller -
import {
Controller,
Get,
Post,
Body,
Param,
Delete,
Logger,
} from '#nestjs/common';
import { BatchService } from './batch.service';
import { Batch } from './batch.entity';
import { InsertResult } from 'typeorm';
#Controller('batch')
export class BatchController {
private readonly logger = new Logger(BatchController.name);
constructor(private batchService: BatchService) {}
#Post()
create(#Body() batchDto: Batch): Promise<InsertResult> {
this.logger.log(':: BatchController :: create()');
console.log(':: BatchController :: create()');
return this.batchService.create(batchDto);
}
#Get()
findAll(): Promise<Batch[]> {
this.logger.log(':: BatchController :: findAll()');
console.log(':: BatchController :: findAll()');
return this.batchService.findAll();
}
#Get(':batch')
findOne(#Param('batch') batchName): Promise<Batch> {
this.logger.log(':: BatchController :: findOne()');
console.log(':: BatchController :: findOne()');
return this.batchService.findOne(batchName);
}
#Delete(':batch')
remove(#Param('batch') batchName) {
this.logger.log(':: BatchController :: remove()');
console.log(':: BatchController :: remove()');
return this.batchService.remove(batchName);
}
}
Put the console and logger both as to see if anything works.
It seems like you have the logs disabled for your NestJS Application.
The best and simple way to enable NestJS request logging is to use its built-in logger service.
To do that, you can pass an array of LogLevels when bootstrapping a NestJS Application.
In your main.ts OR wherever you are bootstrapping the Nest app,
const app = await NestFactory.create<NestExpressApplication>(AppModule, new ExpressAdapter(), {
cors: true,
logger: ['error', 'warn', 'log'] // <--- Add this line in options object
});
Here, pass an array of LogLevels in options.logger.
As of now, LogLevel type has these applicable values:
declare type LogLevel = 'log' | 'error' | 'warn' | 'debug' | 'verbose';
(code snippet taken from #nestjs/common)
Now run the project again and it should print request logs in console.
Almost sure that there are some host settings that prevent the normal behavior. I will contact my hosting provider to understand it, but how did i get to that information:
My NestJS app stopped to send emails. I implemented Logger from NestJS tutorial - No output.
I have added console.log/error - no output
I have removed all my app files from host and left only 1 app.js file with a simple code snippet:
const http = require("http");
const port = 80;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end("Hello World");
});
server.listen(port, () => {
console.log("I WILL BE LOGGED1?");
process.stdout.write("I WILL BE LOGGED2?");
process.stderr.write("I WILL BE LOGGED3?");
});
And noticed that only "process.stderr.write" gets logged no matter what you do. So currently i'm using only that piece of code to log info.
I was not able to get either console.log nor this.logger.log to work. After some sanity checking, it appeared to be a cacheing issue.
In my case the solution was much simpler. I had to:
delete the /dist folder and
re-run npm run start:dev
Both console.log(...) and this.logger.log(...) instance I setup began working.
I cant seem to locate where the error is coming from as the app compiled with Found 0 errors. Watching for file changes. I have seen similar resolve on StackOverflow but none seem to address the issue
Here is the stack trace
internal/modules/cjs/loader.js:797
throw err;
^
Error: Cannot find module './app.controller'
Require stack:
- C:\Users\DELL\Documents\DokunFiles\Nestjs\app\api\dist\src\app.module.js
- C:\Users\DELL\Documents\DokunFiles\Nestjs\app\api\dist\src\main.js
With the appModule below, the app controller is properly imported into the app module
app.module.ts
import { Module } from '#nestjs/common';
import { MongooseModule } from '#nestjs/mongoose';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UserModule } from './user/user.module';
#Module({
imports: [MongooseModule.forRoot(process.env.MONGO_URI,
{
useNewUrlParser: true,
useUnifiedTopology: true
})
, UserModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule { }
app.controller.ts
import { Controller, Get } from '#nestjs/common';
import { AppService } from './app.service';
#Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
#Get()
getHello(): string {
return this.appService.getHello();
}
}
Run:
npm run prebuild
or
rimraf dist
or
rm -rf dist/
And try again.
I had the same issue:
stanislas#yeji > nest start api -> master ! ? RC=130
internal/modules/cjs/loader.js:983
throw err;
^
Error: Cannot find module './app.controller'
Require stack:
- /Users/stanislas/git/soundbase/api/dist/src/app.module.js
- /Users/stanislas/git/soundbase/api/dist/src/main.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
at Function.Module._load (internal/modules/cjs/loader.js:862:27)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/Users/stanislas/git/soundbase/api/dist/src/app.module.js:13:26)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Module.require (internal/modules/cjs/loader.js:1042:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/stanislas/git/soundbase/api/dist/src/app.module.js',
'/Users/stanislas/git/soundbase/api/dist/src/main.js'
]
}
And noticed the controller was indeed not compiled:
stanislas#yeji > ll dist/src/ api -> master ! ?
total 48
-rw-r--r-- 1 stanislas staff 138B Apr 19 17:45 app.module.d.ts
-rw-r--r-- 1 stanislas staff 2.1K Apr 19 17:45 app.module.js
-rw-r--r-- 1 stanislas staff 753B Apr 19 17:45 app.module.js.map
-rw-r--r-- 1 stanislas staff 11B Apr 19 17:45 main.d.ts
-rw-r--r-- 1 stanislas staff 340B Apr 19 17:45 main.js
-rw-r--r-- 1 stanislas staff 290B Apr 19 17:45 main.js.map
drwxr-xr-x 5 stanislas staff 160B Apr 19 17:52 migration
But I noticed it worked as prod:
stanislas#yeji > npm run build && npm run start:prod api -> master ! ? RC=1
> soundbase-api#0.0.1 prebuild /Users/stanislas/git/soundbase/api
> rimraf dist
> soundbase-api#0.0.1 build /Users/stanislas/git/soundbase/api
> nest build
> soundbase-api#0.0.1 start:prod /Users/stanislas/git/soundbase/api
> node dist/main
[Nest] 95175 - 04/19/2020, 6:40:45 PM [NestFactory] Starting Nest application...
[Nest] 95175 - 04/19/2020, 6:40:45 PM [InstanceLoader] PassportModule dependencies initialized +34ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [InstanceLoader] JwtModule dependencies initialized +1ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [InstanceLoader] AuthModule dependencies initialized +0ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [InstanceLoader] AppModule dependencies initialized +1ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [InstanceLoader] TypeOrmCoreModule dependencies initialized +56ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [InstanceLoader] UsersModule dependencies initialized +0ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RoutesResolver] AppController {}: +3ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RouterExplorer] Mapped {/auth/login, POST} route +3ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RouterExplorer] Mapped {/profile, GET} route +0ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RoutesResolver] UserController {/users}: +0ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RouterExplorer] Mapped {/users, POST} route +1ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RouterExplorer] Mapped {/users, GET} route +0ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RouterExplorer] Mapped {/users/:id, GET} route +1ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RouterExplorer] Mapped {/users/:id, PUT} route +0ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RouterExplorer] Mapped {/users/:id, DELETE} route +0ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [NestApplication] Nest application successfully started +2ms
And then it automagically started working again:
stanislas#yeji > npm start api -> master ! ? RC=130
> soundbase-api#0.0.1 start /Users/stanislas/git/soundbase/api
> nest start
[Nest] 95255 - 04/19/2020, 6:41:00 PM [NestFactory] Starting Nest application...
[Nest] 95255 - 04/19/2020, 6:41:00 PM [InstanceLoader] PassportModule dependencies initialized +35ms
[Nest] 95255 - 04/19/2020, 6:41:00 PM [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 95255 - 04/19/2020, 6:41:00 PM [InstanceLoader] JwtModule dependencies initialized +0ms
[Nest] 95255 - 04/19/2020, 6:41:00 PM [InstanceLoader] AuthModule dependencies initialized +1ms
[Nest] 95255 - 04/19/2020, 6:41:00 PM [InstanceLoader] AppModule dependencies initialized +0ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [InstanceLoader] TypeOrmCoreModule dependencies initialized +56ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [InstanceLoader] UsersModule dependencies initialized +1ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RoutesResolver] AppController {}: +3ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RouterExplorer] Mapped {/auth/login, POST} route +2ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RouterExplorer] Mapped {/profile, GET} route +1ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RoutesResolver] UserController {/users}: +0ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RouterExplorer] Mapped {/users, POST} route +1ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RouterExplorer] Mapped {/users, GET} route +0ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RouterExplorer] Mapped {/users/:id, GET} route +0ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RouterExplorer] Mapped {/users/:id, PUT} route +1ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RouterExplorer] Mapped {/users/:id, DELETE} route +0ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [NestApplication] Nest application successfully started +2ms
Since I already tried nest build && node dist/main.js beforehand, I'm pretty sure npm run prebuild aka rimraf dist (which just rm -rf dist/) did the trick. Since the issue was in the dist folder, that seems logical enough for me. Not sure what the exact issue was though.
Look for "incremental": true in your tsconfig.json file. The true makes incremental updates. Setting it to false hopefully will save you having to clean dist every time
Check file extension. In my case file with controller has .js extension, but must .ts
After creation of custom lib from the official documentation
nest g library my-library
AND even running
npm run prebuild
I was always getting the same error because of the files ("libs" dir) outside of src that were being compiled.
Here was my dist folder:
I solved with adding "libs" for excluded files in tsconfig.build.ts