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

Node Server doesn't start automatically #85

Closed
kefniark opened this issue Apr 14, 2023 · 7 comments
Closed

Node Server doesn't start automatically #85

kefniark opened this issue Apr 14, 2023 · 7 comments

Comments

@kefniark
Copy link

kefniark commented Apr 14, 2023

Description

I've been using vite-plugin-node with express and both REST and GraphqlAPI for few month and it works quite well so far.
But there is one thing that annoy me and I wonder if there is a way through config to do something.

Basically when I use vite dev, I get the vite log

VITE v4.0.4  ready in 335 ms
api:start:
api:start:   ➜  Local:   http://localhost:3200/
api:start:   ➜  Network: http://172.18.0.4:3200/

But probably because of the HMR, the server and code are only executed when a query is done.
So starting the server, only start vite, not really the node server, it need a query to finally notice and start the API.

It's really annoying from a dev perspective because:

  • the first query is always really slow (it boot everything, connect to db, ...), even if it had 2-3 min to do it before.
  • if there are some config, deps issues, we don't notice the problem unless a query is made to the server

Is there a way to force vite to execute the viteNodeApp automatically on startup?

I thought about adding some healthcheck logic around to request a useless endpoint like /health.
But it felt like an ugly workaround.

@kefniark kefniark changed the title Server doesn't start Node Server doesn't start automatically Apr 14, 2023
@pquoctuanno1
Copy link

Hi @xmuller. I have read through and tried to apply your method, but I think it has some problems. I only want to connect to the db when fastify is ready.

if (import.meta.env.DEV) {
	fastify.addHook('onReady', onReady)
}
if (import.meta.env.PROD) {
	fastify.listen({ host: '0.0.0.0', port: 10000 }).then(onReady)
}

So your way the function "onReady" will not be executed, your way only executes functions that run directly in the file "config.appPath"

@xmuller
Copy link

xmuller commented May 7, 2023

Hello !

Yes, to be fair I didn't think a lot about the code I wrote.
My pull request works probably only in my case which is to init my NestJs backend at startup to check modules instanciation.

In the end I don't use this plugin. I do the classic build and nodemon with https://github.com/SukkaW/rollup-plugin-swc.
I will probably close my pull request because it's clear that this code has flaws.

As far as my research goes, the only benefit of another plugin would be to have a dev environnement that integrate the HMR API with one or many backend.
Nothing seems to prevent it but I don't know if it's worth the effort 🤷‍♂️

@xmuller
Copy link

xmuller commented May 7, 2023

Hi @xmuller. I have read through and tried to apply your method, but I think it has some problems. I only want to connect to the db when fastify is ready.

if (import.meta.env.DEV) {
	fastify.addHook('onReady', onReady)
}
if (import.meta.env.PROD) {
	fastify.listen({ host: '0.0.0.0', port: 10000 }).then(onReady)
}

So your way the function "onReady" will not be executed, your way only executes functions that run directly in the file "config.appPath"

By default the only way the vite server load modules is from the handler given here: https://github.com/axe-me/vite-plugin-node/blob/main/packages/vite-plugin-node/src/server/fastify.ts based on the viteNodeApp that is exported from your main file.

If you want to load execute something when fastify is ready you can try to call it yourself first.

if (import.meta.env.DEV) {
	fastify.addHook('onReady', onReady)
        await fastify.ready()
}
export const fastify as viteNodeApp

There is a high chance that this will not work. That why I choose to not bother me and just compile and run which is as fast as the method proposed in this plugin.

@GeoffreyBooth
Copy link

GeoffreyBooth commented Jun 14, 2023

I have a Node app that doesn’t listen at all, so I needed a way to start it when run via vite-plugin-node. This is what I came up with:

vite.config.js

import { defineConfig } from 'vite'
import { VitePluginNode as vitePluginNode } from 'vite-plugin-node'


export default defineConfig({
	build: {
		lib: {
			entry: new URL('lib/main.js', import.meta.url).pathname,
		},
	},
	server: {
		port: 3000,
	},
	plugins: [
		...vitePluginNode({
			adapter({ app, req, res }) {
				app(req, res)
			},
			appPath: './entry.js',
			exportName: 'viteNodeApp',
		}),
	],
})

// Trigger server initialization by making a request to it; 
// and keep restarting it after HMR by sending new requests:
setInterval(() => {
	try {
		fetch('http://localhost:3000')
	} catch {}
}, 1000)

entry.js

let initialized = false
/**
 * Export for vite-plugin-node.
 */
export function viteNodeApp(_request, response) {
	if (!initialized) {
		initialized = true
		initialize()
	}

	response.statusCode = 200
	response.end()
}

async function initialize() {
	// App goes here
}

@axe-me
Copy link
Owner

axe-me commented Nov 8, 2023

Sorry I'm late to the party. I just added an initAppOnBoot option, when it is set to true, your app will be loaded once the vite server starts. it's released in 3.1.0, please upgrade and test it out, let me know if it works for you please.

@axe-me
Copy link
Owner

axe-me commented Nov 9, 2023

closing this for now, feel free to reopen it if you have any questions.

@axe-me axe-me closed this as completed Nov 9, 2023
@PurpleTape
Copy link

PurpleTape commented Nov 24, 2023

Hello @axe-me! I'm facing a similar problem. I'm trying to use this together with Nest (initAppOnBoot: true):

  VITE v5.0.2  ready in 180 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: http://192.168.0.199:5173/
  ➜  press h + enter to show help

[Nest] 15975  - 11/24/2023, 2:50:14 PM     LOG [NestFactory] Starting Nest application...
[Nest] 15975  - 11/24/2023, 2:50:14 PM     LOG [InstanceLoader] AppModule dependencies initialized +16ms
[Nest] 15975  - 11/24/2023, 2:50:14 PM     LOG [InstanceLoader] NestjsGrammyModule dependencies initialized +0ms
[Nest] 15975  - 11/24/2023, 2:50:14 PM     LOG [InstanceLoader] DiscoveryModule dependencies initialized +0ms
[Nest] 15975  - 11/24/2023, 2:50:15 PM     LOG [InstanceLoader] GrammyCoreModule dependencies initialized +368ms
[Nest] 15975  - 11/24/2023, 2:50:15 PM     LOG [InstanceLoader] BotsModule dependencies initialized +0ms
[Nest] 15975  - 11/24/2023, 2:50:57 PM     LOG [NestApplication] Nest application successfully started +42153ms

But the application finally starts (log: LOG [NestApplication] Nest application successfully started +42153ms) only when I manually go to the address http://192.168.0.199:5173/ in the browser.

What can be done in such a situation?

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

6 participants