-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fastify Support? #3
Comments
This seems to work. By default in Fastify 3 it doesn't support connect style middleware. import chalk from 'chalk';
import debug from 'debug';
import fastify, { FastifyInstance } from 'fastify';
import { Server } from 'http';
import middie from 'middie';
import { IServer } from 'vite-plugin-node';
export const debugFastify = debug("vite:node-plugin:fastify");
export const FastifyServer: IServer<FastifyInstance> = {
_app: undefined,
_server: undefined,
_config: undefined,
async create(server, config) {
this._config = config;
const { createViteNodeApp } = await server.ssrLoadModule(
this._config.appPath
);
this._app = createViteNodeApp as FastifyInstance;
await this._app.register(middie);
this._app.use(server.middlewares);
debugFastify(chalk.dim`app created`);
},
async start() {
await this._app?.listen(this._config?.port as number);
debugFastify(chalk.dim`server started at port ${this._config?.port}`);
this._server = this._app;
},
async close() {
await (this._server as Server)?.close();
debugFastify(chalk.dim`server closed`);
},
async restart() {
debugFastify(chalk.dim`server restarting`);
if (this._server) {
await this.close();
}
await this.start();
},
}; |
I'm working on this as well. I got the below to work at least for the basics....still have to do more digging into it but this could be fantastic to use vite createCustomServer: () => {
const FastifyServer: IServer<FastifyInstance, Server> = {
_app: undefined,
_server: undefined,
_config: undefined,
async create(server, config) {
this._config = config;
const { createViteNodeApp } = await server.ssrLoadModule(this._config.appPath);
this._app = (await createViteNodeApp) as FastifyInstance;
console.debug(`app created`);
},
async start() {
console.log('server starting');
await this._app?.listen(this._config?.port as number);
console.debug(`server started at port ${this._config?.port}`);
this._server = this._app;
},
async close() {
console.debug('app trying to close');
if (this._app && this._server) {
// await this._app?.close();
await this._server.close();
}
console.debug(`server closed`);
},
async restart() {
console.debug(`server restarting`);
await this.close();
await this.start();
},
};
return FastifyServer;
}, |
Hi @delaneyj @erossdev, thanks for your interest to use this plugin. I recently rewrote how this plugin works to make it faster by not restarting the server on file update. If you care to know more detail, see https://github.com/axe-me/vite-plugin-node#how While I was rewriting it, I tried to add fastify support. Unfortunately, due to I never used it and didn't find a way how can I by pass a node HTTP request to its app in short time, in the new release I still not have it included. Any helps from you fastify experts would be appreciated. The basic idea is I need to pass down all the requests from one fastify app to another, something like: fastify.addHook('onRequest', async (req, res) => {
// get the latest app instance
const { viteNodeApp } = await server.ssrLoadModule(config.appPath)
// pass down requests, here is where I don't know how to implement in fastify
viteNodeApp(req, res)
}) |
closing this as I just figured out a way to make fastify works with this plugin. It's released in 0.0.10, please try it out! |
So far so good! Works great, super fast and reloading seems to work too. Thanks I really appreciate it! |
Would the 'nest' mode work for direct fastify support?
The text was updated successfully, but these errors were encountered: