Skip to content

Commit

Permalink
Merge pull request #464 from thisismydesign/424-catch-all-route
Browse files Browse the repository at this point in the history
424-catch-all-route
  • Loading branch information
thisismydesign authored Oct 26, 2023
2 parents 0927239 + c948cd9 commit 99d1152
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 45 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "nest-typescript-starter",
"name": "nestjs-nextjs-mvc-starter",
"private": true,
"version": "1.0.0",
"description": "Nest TypeScript starter repository",
Expand Down
12 changes: 6 additions & 6 deletions src/client/pages/home.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import React from 'react';
import { NextPage } from 'next';

const Home: NextPage<{ data: string }> = (props) => {
const { data } = props;
const Home: NextPage<{ message: string; query: string }> = (props) => {
const { message, query } = props;

return (
<div>
<h1>Hello from NextJS! - Home</h1>
{data}
{message}
{query}
</div>
);
};

Home.getInitialProps = ({ query }) => {
return {
data: `some initial props including query params and controller data: ${JSON.stringify(
query,
)}`,
message: 'some initial props including query params',
query: JSON.stringify(query),
};
};

Expand Down
44 changes: 8 additions & 36 deletions src/server/view/view.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Controller, Get, Res, Req, UseGuards } from '@nestjs/common';
import { Request, Response } from 'express';
import { parse } from 'url';
import { JwtAuthGuard } from '../app/auth/jwt/jwt-auth.guard';

import { ViewService } from './view.service';
Expand All @@ -9,45 +8,18 @@ import { ViewService } from './view.service';
export class ViewController {
constructor(private viewService: ViewService) {}

async handler(req: Request, res: Response) {
const parsedUrl = parse(req.url, true);
await this.viewService
.getNextServer()
.render(req, res, parsedUrl.pathname, parsedUrl.query);
@Get('_next*')
public async assets(@Req() req: Request, @Res() res: Response) {
await this.viewService.handler(req, res);
}

@Get('home')
public async showHome(@Req() req: Request, @Res() res: Response) {
const parsedUrl = parse(req.url, true);
const serverSideProps = { dataFromController: '123' };

await this.viewService
.getNextServer()
.render(
req,
res,
parsedUrl.pathname,
Object.assign(parsedUrl.query, serverSideProps),
);
public async home(@Req() req: Request, @Res() res: Response) {
await this.viewService.handler(req, res);
}

@UseGuards(JwtAuthGuard)
@Get('profile')
public async showProfile(@Req() req: Request, @Res() res: Response) {
await this.handler(req, res);
}

@UseGuards(JwtAuthGuard)
@Get('orders')
public async indexOrders(@Req() req: Request, @Res() res: Response) {
await this.handler(req, res);
}

@Get('_next*')
public async assets(@Req() req: Request, @Res() res: Response) {
const parsedUrl = parse(req.url, true);
await this.viewService
.getNextServer()
.render(req, res, parsedUrl.pathname, parsedUrl.query);
@Get('/:path((?!graphql$))*')
public async authenticatedPage(@Req() req: Request, @Res() res: Response) {
await this.viewService.handler(req, res);
}
}
5 changes: 3 additions & 2 deletions src/server/view/view.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import createServer from 'next';
import { Request, Response } from 'express';
import { NextServer } from 'next/dist/server/next';

@Injectable()
Expand All @@ -21,7 +22,7 @@ export class ViewService implements OnModuleInit {
}
}

getNextServer(): NextServer {
return this.server;
handler(req: Request, res: Response) {
return this.server.getRequestHandler()(req, res);
}
}

0 comments on commit 99d1152

Please sign in to comment.