Skip to content
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

Closed
delaneyj opened this issue Jun 2, 2021 · 5 comments
Closed

Fastify Support? #3

delaneyj opened this issue Jun 2, 2021 · 5 comments

Comments

@delaneyj
Copy link

delaneyj commented Jun 2, 2021

Would the 'nest' mode work for direct fastify support?

@delaneyj
Copy link
Author

delaneyj commented Jun 2, 2021

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();
  },
};

@erossdev
Copy link

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;
},

@axe-me
Copy link
Owner

axe-me commented Aug 11, 2021

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)
})

@axe-me
Copy link
Owner

axe-me commented Aug 11, 2021

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!

@axe-me axe-me closed this as completed Aug 11, 2021
@erossdev
Copy link

So far so good! Works great, super fast and reloading seems to work too. Thanks I really appreciate it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants