How to serve static files in Node js using Hono js

0 votes
How to serve static files in Node.js using Hono.js?

I'm trying to serve static files in a Node.js application using Hono.js. Can someone help me with setting this up?
Dec 12, 2024 in Web Development by Nidhi
• 7,560 points
100 views

1 answer to this question.

0 votes

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

answered Dec 12, 2024 by Navya

Related Questions In Web Development

0 votes
1 answer

How To Implement Caching in Node.js Using Redis?

To retrieve cached data from Redis in ...READ MORE

answered Oct 25, 2024 in Web Development by kavya
164 views
0 votes
1 answer

How to set a cookie in Node.js using the Express framework?

You can use the res.cookie() method to ...READ MORE

answered Nov 27, 2024 in Web Development by Navya
104 views
0 votes
0 answers

How to minimize .js files in a Node.js application?

How to minimize .js files in a ...READ MORE

Nov 26, 2024 in Web Development by Nidhi
• 7,560 points
67 views
0 votes
0 answers

How to upload a file to api server in node js?

How to upload a file to api ...READ MORE

Oct 14, 2024 in Web Development by anonymous
• 7,560 points
129 views
0 votes
1 answer

how to safely deploy npm install without it causing inconsistencies?

The recent versions on npm generates a ...READ MORE

answered Apr 11, 2018 in DevOps on Cloud by DareDev
• 6,890 points
1,072 views
0 votes
1 answer

Unable to request channel creation using Rest Api

I'd recommend taking a look at the ordering ...READ MORE

answered Jul 16, 2018 in Blockchain by Perry
• 17,100 points
978 views
0 votes
1 answer

How do you serve static files efficiently using Express.js?

1. Use the express.static middleware: This is the ...READ MORE

answered Oct 28, 2024 in Web Development by kavya
206 views
0 votes
1 answer

How do you serve static files efficiently using Express.js?

Serving static files effectively using Express.js Use express.static() ...READ MORE

answered Dec 4, 2024 in Web Development by Navya
60 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP