honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
18.52k stars 522 forks source link

Support SPA like express #3124

Closed ckken closed 2 months ago

ckken commented 2 months ago

What is the feature you are proposing?

app.use(express.static(staticRoot))//dist app.get('*', (req, res) => res.send(html))//html in dist

EdamAme-x commented 2 months ago

Hi @ckken hmm....

I think it was already implemented in Hono.

app.use(serveStatic({ root: staticPath }) }) // dist
app.get('*', (c) => c.html(html)) // html in dist
ckken commented 2 months ago

we use hono replace express in webpack cli,Absolute root path must be use in express.static

EdamAme-x commented 2 months ago

hmm... I think it's unnecessary. You can use a relative path.

app.use(serveStatic({ root: "./dist" }) }) // dist
app.get('*', (c) => c.html(html)) // html in dist

If we don't think the problem of performance, you can use .mount()

ckken commented 2 months ago

The cli does not exist in the project, but is installed through npm, while hono is integrated into the cli, so there are similar demos

EdamAme-x commented 2 months ago

I may be mistaken. Do you want to migrate express to hono? Do you want to migrate hono to express? Or another? @ckken

ckken commented 2 months ago

like this https://github.com/honojs/hono/issues/3107

EdamAme-x commented 2 months ago

hmm... I'm not quite sure what the intent is. sorry

please explain the details?

ckken commented 2 months ago

fixed it!

import {serveStatic} from 'hono/serve-static'
app.use(
      '*',
      serveStatic({
        root: '/',
        pathResolve(filePath: string) {
          return path.resolve(store.outDir, filePath)
        },
        getContent(filePath, c) {
          return fsp.readFile(filePath)
        },
      }),
    )
EdamAme-x commented 2 months ago

Good, I'm relieved.