Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a static web page to a Vapor server

Tags:

vapor

I have a Vapor server API running in Heroku supporting an iOS app. I want to create a simple landing page for my app and I would like to host it in my existing Vapor server. How could I do that?

like image 236
Carlos Avatar asked Oct 28 '25 04:10

Carlos


1 Answers

Vapor actually has a built-in middleware that makes this very easy. First, make sure you have a Public directory at the root of your Vapor project. Then you can put your static HTML page in there, along with any CSS and JS files it might rely on.

Next, you just need to add FileMiddleware to your application's middleware (docs):

let file = FileMiddleware(publicDirectory: app.directory.publicDirectory)
app.middleware.use(file)

Now you can access any of the files in your Public directory using their relative directory path as the path in the URL to your app. For example, if you have a static directory in your Public directory, and put a home.html file in it, you request the page by going to http://localhost:8080/static/home.html in your browser.

like image 199
Caleb Kleveter Avatar answered Oct 30 '25 17:10

Caleb Kleveter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!