Rocket: StaticFiles serving

Created on 8 Dec 2018  路  12Comments  路  Source: SergioBenitez/Rocket

Hi,

I am following static files serving example, however compiler is complaining about below issue

error[E0432]: unresolved import rocket_contrib::serve
--> src/main.rs:12:21
|
12 | use rocket_contrib::serve::StaticFiles;
| ^^^^^ could not find serve in rocket_contrib

Not sure if examples needs to be updated

Regards,
Suhas

invalid

Most helpful comment

You need to enable the serve feature. This is in the docs.

All 12 comments

You need to enable the serve feature. This is in the docs.

thank you. Indeed, I lately referred Cargo.toml. Although its working now browser logs shows below errors to me

localhost/:161 GET http://localhost:8000/static/vendor/bootstrap/js/bootstrap.bundle.min.js net::ERR_ABORTED 500 (Internal Server Error)
localhost/:164 GET http://localhost:8000/static/js/clean-blog.min.js net::ERR_ABORTED 500 (Internal Server Error)
localhost/:14 GET http://localhost:8000/static/css/clean-blog.min.css net::ERR_ABORTED 500 (Internal Server Error)
localhost/:9 GET http://localhost:8000/static/vendor/fontawesome-free/css/all.min.css net::ERR_ABORTED 500 (Internal Server Error)
localhost/:6 GET http://localhost:8000/static/vendor/bootstrap/css/bootstrap.min.css net::ERR_ABORTED 500 (Internal Server Error)
localhost/:160 GET http://localhost:8000/static/vendor/jquery/jquery.min.js net::ERR_ABORTED 500 (Internal Server Error)
localhost/:160 GET http://localhost:8000/static/vendor/jquery/jquery.min.js net::ERR_ABORTED 500 (Internal Server Error)
localhost/:161 GET http://localhost:8000/static/vendor/bootstrap/js/bootstrap.bundle.min.js net::ERR_ABORTED 500 (Internal Server Error)
localhost/:164 GET http://localhost:8000/static/js/clean-blog.min.js net::ERR_ABORTED 500 (Internal Server Error)

Not sure why are these error

Those don't look like Rocket's log messages.

Here are few logs from rocket

GET /static/vendor/bootstrap/css/bootstrap.min.css text/css:
=> Matched: GET / => Warning: Response was None.
=> Outcome: Failure
=> Warning: Responding with 404 Not Found catcher.
=> Response succeeded.
GET /static/vendor/fontawesome-free/css/all.min.css text/css:
=> Matched: GET / => Warning: Response was None.
=> Outcome: Failure
=> Warning: Responding with 404 Not Found catcher.
GET /static/css/clean-blog.min.css text/css:
=> Matched: GET / => Warning: Response was None.
=> Outcome: Failure
=> Warning: Responding with 404 Not Found catcher.
=> Response succeeded.
GET /static/vendor/jquery/jquery.min.js:
=> Matched: GET / => Warning: Response was None.
=> Outcome: Failure
=> Warning: Responding with 404 Not Found catcher.
=> Response succeeded.
GET /static/vendor/bootstrap/js/bootstrap.bundle.min.js:
GET /static/js/clean-blog.min.js:
=> Matched: GET / => => Warning: Response was None.
=> Outcome: Failure
=> Warning: Responding with 404 Not Found catcher.
=> Matched: GET / => Warning: Response was None.
=> Outcome: Failure
=> Warning: Responding with 404 Not Found catcher.

Below is the code
.mount("/", StaticFiles::from("static/"))

Are you starting your application in a directory that contains the static/ directory? The docs mention this.

No, I was using handlebar templates example which has templates directory, inside template I have created static directory and placed JS and CSS over there.

created static folder in the same directory and tried to execute the project, however it results are same no luck.

@suhaschitade Try .mount("/", StaticFiles::from("static")). That is static without the trailing /.

Thank you. I tried, however no change in results.

@suhaschitade Sorry, I don't know how to help you. The unit tests and my local testing confirm that everything works as expected. Please read the documentation closely.

thanks for your help. I followed todo example and created static directory and place all html css js there it seems to be working now. Although I would really want to keep HTML separate from css, js it seems its not possible at the moment.
Regards,

@suhaschitade There is no such restriction. You can place files wherever you like.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sphinxc0re picture sphinxc0re  路  3Comments

paulvt picture paulvt  路  4Comments

Perseus101 picture Perseus101  路  4Comments

klnusbaum picture klnusbaum  路  4Comments

lucklove picture lucklove  路  4Comments