Git Product home page Git Product logo

Comments (5)

axe-me avatar axe-me commented on June 19, 2024 1

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!

from vite-plugin-node.

erossdev avatar erossdev commented on June 19, 2024 1

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

from vite-plugin-node.

delaneyj avatar delaneyj commented on June 19, 2024

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

from vite-plugin-node.

erossdev avatar erossdev commented on June 19, 2024

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

from vite-plugin-node.

axe-me avatar axe-me commented on June 19, 2024

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

from vite-plugin-node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.