title: Serving static files with actix-web 2.0
body: <p>I'm struggling with actix-web 2.0 framework of rust. I want my rust server to serve the my index.html file but most of the help available is of older versions and hence a lot has changed in newer version. I tried following code but it's not working for actix-web 2.0. Please suggest some working solution in actix-web 2.0.</p>
  <pre><code>use actix_files::NamedFile;
  use actix_web::{HttpRequest, Result};
  async fn index(req: HttpRequest) -&gt; Result&lt;NamedFile&gt; {
      Ok(NamedFile::open(path_to_file)?)
  }
  </code></pre>
  <hr />
  <p>By trying the code given in the answer I could serve a single html file but it is unable to load the linked JavaScript file. I have tried the following approach suggested in <a href="https://actix.rs/docs/static-files/" rel="nofollow noreferrer">https://actix.rs/docs/static-files/</a> to serve the directory.</p>
  <pre><code>#[actix_rt::main]
  async fn main() -&gt; std::io::Result&lt;()&gt; {
      dotenv::dotenv().ok();
      std::env::set_var(&quot;RUST_LOG&quot;, &quot;actix_web=debug&quot;);
      let database_url = std::env::var(&quot;DATABASE_URL&quot;).expect(&quot;set DATABASE_URL&quot;);
  
      // create db connection pool
      let manager = ConnectionManager::&lt;PgConnection&gt;::new(database_url);
      let pool: Pool = r2d2::Pool::builder()
          .build(manager)
          .expect(&quot;Failed to create pool.&quot;);
      
      //Serving the Registration and sign-in page
      async fn index(_req: HttpRequest) -&gt; Result&lt;NamedFile&gt; {
          let path: PathBuf = &quot;./static/index.html&quot;.parse().unwrap();
          Ok(NamedFile::open(path)?)
      }
  
      // Start http server
      HttpServer::new(move || {
          App::new()
              .data(pool.clone())
              .service(fs::Files::new(&quot;/static&quot;, &quot;.&quot;).show_files_listing())
              .route(&quot;/&quot;, web::get().to(index))
              .route(&quot;/users&quot;, web::get().to(handler::get_users))
              .route(&quot;/users/{id}&quot;, web::get().to(handler::get_user_by_id))
              .route(&quot;/users&quot;, web::post().to(handler::add_user))
              .route(&quot;/users/{id}&quot;, web::delete().to(handler::delete_user))
      })
      .bind(&quot;127.0.0.1:8080&quot;)?
      .run()
      .await
  }
  </code></pre>
  <p>Above is my main method. In browser console I'm still getting the error that unable to load the Registration.js resource. Following is my folder structure:</p>
  <pre class="lang-none prettyprint-override"><code>-migrations
  -src
    -main.rs
    -handler.rs
    -errors.rs
    -models.rs
    -schema.rs
  -static
   -index.html
   -Registration.js
  -target
  Cargo.toml
  .env
  Cargo.lock
  diesel.toml
  </code></pre>
  <p>I have already built the backend with DB integration and it is working fine as checked by curl commands and now I'm trying to build front end and as first step trying to serve static files.</p>