Skip to content

Commit

Permalink
-Next build for production serve
Browse files Browse the repository at this point in the history
-Prettier fixes in tsx
-Removed unused AppController and AppService
-Moved Client App Logic to Main.ts
-Created Contact List Module
  • Loading branch information
hnviradiya committed Jan 7, 2022
1 parent b886e83 commit 5e23030
Show file tree
Hide file tree
Showing 19 changed files with 112 additions and 124 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"build": "nest build && cd src/client && next build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
Expand Down
14 changes: 7 additions & 7 deletions src/client/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { NextPage } from 'next'
import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'
import type { NextPage } from 'next';
import Head from 'next/head';
import Image from 'next/image';
import styles from '../styles/Home.module.css';

const Home: NextPage = () => {
return (
Expand Down Expand Up @@ -66,7 +66,7 @@ const Home: NextPage = () => {
</a>
</footer>
</div>
)
}
);
};

export default Home
export default Home;
22 changes: 0 additions & 22 deletions src/server/app.controller.spec.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/server/app.controller.ts

This file was deleted.

8 changes: 2 additions & 6 deletions src/server/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ClientAppModule } from './client-app/client-app.module';
import { ContactListModule } from './contact-list/contact-list.module';

@Module({
imports: [ClientAppModule],
controllers: [AppController],
providers: [AppService],
imports: [ContactListModule],
})
export class AppModule {}
8 changes: 0 additions & 8 deletions src/server/app.service.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/server/client-app/client-app.controller.spec.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/server/client-app/client-app.controller.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/server/client-app/client-app.module.ts

This file was deleted.

27 changes: 0 additions & 27 deletions src/server/client-app/client-app.service.ts

This file was deleted.

18 changes: 18 additions & 0 deletions src/server/contact-list/contact-list.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ContactListController } from './contact-list.controller';

describe('ContactListController', () => {
let controller: ContactListController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ContactListController],
}).compile();

controller = module.get<ContactListController>(ContactListController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
12 changes: 12 additions & 0 deletions src/server/contact-list/contact-list.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { ContactListService } from './contact-list.service';

@Controller('contact-list')
export class ContactListController {
constructor(private readonly contactListService: ContactListService) {}

@Get()
getContacts(): Array<any> {
return this.contactListService.getContacts();
}
}
9 changes: 9 additions & 0 deletions src/server/contact-list/contact-list.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { ContactListController } from './contact-list.controller';
import { ContactListService } from './contact-list.service';

@Module({
controllers: [ContactListController],
providers: [ContactListService],
})
export class ContactListModule {}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ClientAppService } from './client-app.service';
import { ContactListService } from './contact-list.service';

describe('ClientAppService', () => {
let service: ClientAppService;
describe('ContactListService', () => {
let service: ContactListService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ClientAppService],
providers: [ContactListService],
}).compile();

service = module.get<ClientAppService>(ClientAppService);
service = module.get<ContactListService>(ContactListService);
});

it('should be defined', () => {
Expand Down
8 changes: 8 additions & 0 deletions src/server/contact-list/contact-list.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class ContactListService {
getContacts(): Array<any> {
return [];
}
}
12 changes: 12 additions & 0 deletions src/server/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import createServer from 'next/dist/server/next';
import { AppModule } from './app.module';
import { HttpExceptionFilter } from './next.filter';

async function bootstrap() {
const app = await NestFactory.create(AppModule);

const configService = app.get<ConfigService>(ConfigService);
const nextServer = createServer({
dev: configService.get<string>('NODE_ENV') !== 'production',
dir: './src/client',
});
await nextServer.prepare();

app.useGlobalFilters(new HttpExceptionFilter(nextServer));
await app.listen(3000);
}
bootstrap();
23 changes: 23 additions & 0 deletions src/server/next.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ArgumentsHost, ExceptionFilter, HttpException } from '@nestjs/common';
import { NextServer } from 'next/dist/server/next';

export class HttpExceptionFilter implements ExceptionFilter {
constructor(private nextServer: NextServer) {}

public catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const request = ctx.getRequest();
const status = exception.getStatus();

if (status === 404) {
this.nextServer.getRequestHandler()(request, response);
} else {
response.status(status).json({
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
});
}
}
}
2 changes: 1 addition & 1 deletion test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import request from 'supertest';
import { AppModule } from './../src/server/app.module';

describe('AppController (e2e)', () => {
Expand Down
17 changes: 14 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}

0 comments on commit 5e23030

Please sign in to comment.