How do I use remix with NestJs Backend #2755
-
Going through the documentation I found out that remix exposes few server adapters, and there is none for NestJs. I was wondering if there is a way to create a custom server adapter or any existing one already... |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 15 replies
-
If you can put NestJS inside an Express one you could use the Express adapter of Remix. Another option is that you build your own adapter for Nest (you could even publish it to npm so more people could use it!). And a final option is to run two services, one for Nest and another for Remix. |
Beta Was this translation helpful? Give feedback.
-
I would like to go for the second option but no documentation on how to |
Beta Was this translation helpful? Give feedback.
-
@Stancobridge right now you will need to check the code on another adapter, the Express one is probably the most similar to what you will need (https://github.com/remix-run/remix/blob/main/packages/remix-express/server.ts), there's a PR to add an option to pass a custom adapter when creating a project and adds some docs on this. |
Beta Was this translation helpful? Give feedback.
-
I was wondering about this, too. Any updates? |
Beta Was this translation helpful? Give feedback.
-
still waiting on this though |
Beta Was this translation helpful? Give feedback.
-
The new Remix Stack feature let you pass a nix create-remix@latest my-nest-remix-app --template username/remix-nest-adapter-template And get a project with Nest and Remix already configured |
Beta Was this translation helpful? Give feedback.
-
I have been working on adding NestJS as a way to incrementally.. let's say, upgrade, our existing server stack. What I am doing right now is using Nest purely for their Module and Service convention, and utilities surrounding those. You could use the express adapter and a Nest Standalone Application. I haven't figured out how to make Nest and Remix happy when using We are passing services to loaders and actions via |
Beta Was this translation helpful? Give feedback.
-
Actually, I just got it working with NestJS -- or at least, that's what it seems. My solution was to add a global filter to nest via // remix-handler.ts
import path from 'path';
import type { ExceptionFilter, ArgumentsHost } from '@nestjs/common';
import { Catch } from '@nestjs/common';
import type { NextFunction, Request, Response } from 'express';
import type { GetLoadContextFunction } from '@remix-run/express';
import { createRequestHandler } from '@remix-run/express';
const MODE = process.env.NODE_ENV;
const BUILD_DIR = path.join(process.cwd(), 'build');
////////////////////////////////////////////////////////////////////////////////
function purgeRequireCache() {
// purge require cache on requests for "server side HMR" this won't const
// you have in-memory objects between requests in development,
// alternatively you can set up nodemon/pm2-dev to restart the server on
// file changes, we prefer the DX of this though, so we've included it
// for you by default
for (const key in require.cache) {
if (key.startsWith(BUILD_DIR)) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete require.cache[key];
}
}
}
@Catch()
export class RemixHandler implements ExceptionFilter {
catch(_exception: unknown, host: ArgumentsHost): void {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
const next = ctx.getNext<NextFunction>();
const getLoadContext: GetLoadContextFunction = (req) => req.uow;
const handle =
MODE === 'production'
? createRequestHandler({ build: require('../build'), getLoadContext })
: (req: Request, res: Response, next: NextFunction) => {
purgeRequireCache();
return createRequestHandler({ build: require('../build'), mode: MODE, getLoadContext })(
req,
res,
next
);
};
void handle(request, response, next);
}
}
// and then in your main.ts file just do
let app = await NestFactory.create(ServerModule);
app.useGlobalFilters(new RemixHandler());
await app.init(); |
Beta Was this translation helpful? Give feedback.
-
Hi there ! I just added Nest to a Remix app, using the express adapter.
If you don't need Nest's mechanisms such as DI, this controller should be enough . Otherwise you can integrate Nest and Remix further :
There are also other ways to do it. In another project (private unfortunately), I declare all the pages as EDIT : old message, with broken links but most logic is the same. The "Nest to Remix" bridge module source code is here https://github.com/antoinechalifour/remix-hexagonal-architecture/tree/master/src/remix-nest-adapter TLDR :
|
Beta Was this translation helpful? Give feedback.
-
I've developed a straightforward project based on the excellent Remix guide. This project maintains separate directories for Nest and Remix, simplifying updates. It supports Remix Hot Module Replacement (HMR) and offers a unified command for starting the development server. You can view the project here: Nest Remixed. |
Beta Was this translation helpful? Give feedback.
-
You can take a look at my open source projects nest-remix |
Beta Was this translation helpful? Give feedback.
If you can put NestJS inside an Express one you could use the Express adapter of Remix.
Another option is that you build your own adapter for Nest (you could even publish it to npm so more people could use it!).
And a final option is to run two services, one for Nest and another for Remix.