Install Hono.js
If Hono.js is not installed , you can run the given command in your Node.js project:
npm install hono
Create a Static Directory
/public
/index.html
/styles.css
/image.jpg
Set Up the Hono.js App
Next, set up your Hono.js application to serve the static files. You can use Hono's static middleware to serve files from the public directory.
Code Example
Here's an example of how you can serve static files with Hono.js:
const { Hono } = require('hono');
const path = require('path');
const serveStatic = require('@honojs/serve-static');
const app = new Hono();
// Serve static files from the "public" directory
app.use('/static/*', serveStatic({
root: path.join(__dirname, 'public'), // Directory to serve files from
maxAge: 3600, // Optional: Cache static files for 1 hour
}));
// Serve index.html or other default files
app.get('/', (c) => {
return c.html('<h1>Welcome to the static file server!</h1>');
});
app.fire();