Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve static files using Vapor?

Tags:

I'm trying to write server side application using Swift and Vapor framework. However, I can't figure out, how to serve static files using Vapor. It's not enough to just move them to the Public or Resources directory.

How can I do that?

UPD. I performed steps which Tanner Nelson suggested but it still doesn't work.

What I tried so far:

  1. vapor build and vapor run (using Vapor Toolbox v0.6.1).

  2. ./build/debug/App from root directory (which contains Package.swift).

  3. Run in Xcode 8 beta after editing scheme as Tanner Nelson suggested.

In all this cases I get error {"error":true,"message":"Page not found"}

I have file vapor_logo.png inside a Public folder and also the same file inside Public/images/ folder. I try to request it and it fails. Requests that I made: http://localhost:8080/image/vapor_logo.png and http://localhost:8080/vapor_logo.png. However, other routes work fine.

UPD 2. Well, that was all my mistakes. First, file that I think was called vapor_logo.png, actually was called vapor-logo.png. Second, case matters when you make a request. I also tried to request file with name IMG_8235.JPG but write file extension as jpg, so got an error.

So, just to recap: if you experience the same problem as me, follow the Tanner Nelson's answer and make sure that name of requested file exactly matches name of file on disk.

like image 828
Alexander Doloz Avatar asked Jul 11 '16 18:07

Alexander Doloz


People also ask

What is the best way to serve the static files?

To serve static files such as images, CSS files, and JavaScript files, use the express.static built-in middleware function in Express. The root argument specifies the root directory from which to serve static assets.

Where do I serve static files?

Applications often need to serve static files such as JavaScript, images, and CSS in addition to handling dynamic requests. Apps in the standard environment can serve static files from a Google Cloud option like Cloud Storage, serve them directly, or use a third-party content delivery network (CDN).

What are static files used for?

What Are Static Files? Static files are files that don't change when your application is running. These files do a lot to improve your application, but they aren't dynamically generated by your Python web server like a usual HTML response.


1 Answers

Vapor folder structure from Docs:

VaporApp
├── Package.swift
├── Resources
│   ├── Views
│   │   └── hello.leaf
├── Public
│   ├── images (images resources)
│   ├── styles (css resources)
└── Sources
    └── ...

Any files in the Public folder will be served by default if no routes have been registered that conflict with the file name.

For example, if you have a file Public/foo.png and the following main.swift file:

import Vapor

let drop = Droplet()

drop.get("welcome") { request in
    return "Hello, world"
}

drop.serve()

A request to localhost/welcome would return "Hello, world" and a request to localhost/foo.png would return foo.png.

If this is not working properly, it's likely that your working directory is not configured properly. This can happen if you are running your project from Xcode or you are running it from the command line from a folder that is not the root directory of the project.

To fix Xcode, go to Schemes > App > Edit Scheme > Run > Options > Working Directory > [x] Use Custom Working Directory and make sure the directory is set to the root of your project (where the Package.swift resides).

Xcode working directory

To fix when running from the command line, make sure you are running the application from the root directory. i.e., the run command should look something like .build/debug/App since the .build folder is located in the root directory.

like image 64
tanner0101 Avatar answered Sep 19 '22 13:09

tanner0101