how can i use this plugin to restart express #15
-
im using express routes + this package and i set this package to watch my route module folder it does seem to work, but i think the express server has to actually restart as well because the changes in my routes files are not actually showing up on the web app after a reload is there a way to trigger an express server restart using vite + ssr + this plugin? for example, heres my server code: import express from 'express'
import fs from 'fs'
import { globbySync } from 'globby'
import path from 'path'
import { app, env } from 'skele'
import { createServer } from 'vite'
const vite = await createServer({
server: { middlewareMode: true },
appType: 'custom'
})
app.use(vite.middlewares)
app.use((req, res, next) => {
res.locals.originalUrl = req.originalUrl
next()
})
app.use(express.static('public'))
const routeEntries = globbySync('app/routes/**/*.js')
for (const routeEntry of routeEntries) {
await import(path.resolve(routeEntry))
}
const viewsDirectory = 'app/components/pages'
app.engine('jsx', async (filePath, options, callback) => {
const indexHtml = fs.readFileSync('node_modules/skele/src/entries/index.html', 'utf-8')
const transformedHtml = await vite.transformIndexHtml(options._locals.originalUrl, indexHtml)
const { render } = await vite.ssrLoadModule('node_modules/skele/src/entries/ssr.jsx')
const page = filePath.replace(`${path.resolve(viewsDirectory)}/`, '').replace('.jsx', '')
const props = encodeURIComponent(JSON.stringify(options))
const appHtml = await render(page, options)
const rendered = transformedHtml.replace(
'<div id="app"></div>',
`<div id="app" data-page="${page}" data-props="${props}">${appHtml}</div>`,
)
return callback(null, rendered)
})
app.set('views', viewsDirectory)
app.set('view engine', 'jsx')
app.listen(env.APP_PORT, () => {
console.log(`app listening on http://localhost:${env.APP_PORT}`)
}) when i edit a react component HMR shows the changes. however, when i edit a backend route, though vite triggers a reload using this plugin, i don't actually see the changes i made inside the route methods. like i said i think i need to restart express for those changes to show up, but im struggling to figure this out without using something like nodemon. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Based on your use case, it seems that you want to restart the Vite dev server. Try using |
Beta Was this translation helpful? Give feedback.
Based on your use case, it seems that you want to restart the Vite dev server.
Try using
vite-plugin-restart
instead, which was created for a similar use case 😃