1

I'm trying to make a webpage that interacts with JavaScript and sends JSON data to Rust functions, then renders the www files. The structure of my project is:

/..
   /src
   /www
     |___index.html
     |___/css
     |      |__style.css
     |
     |___/javascript
     |             |__javascript-supp.js
     |___/images
               |__myImg.png

main.rs

use nickel::{Nickel, HttpRouter, StaticFilesHandler, NickelError, Request, Response,
             MiddlewareResult};
use std::collections::HashMap;

fn tmpl_handler<'a>(_: &mut Request, res: Response<'a>) -> MiddlewareResult<'a> {
    let mut data = HashMap::<&str, &str>::new();

    // add data for render
    // name = {{ name }} in template
    data.insert("name", "Nickel");

    res.render("www/", &data)
}

fn main() {
    let mut server = Nickel::new();

    server.utilize(StaticFilesHandler::new("www/"));
    server.get("/login/*", tmpl_handler);

    server.listen("127.0.0.1:6767");
}

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src=javascript/javascript.js></script>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <title>Encryptor Test</title>
  </head>

  <body>
<div class=header>
  <h1>
        Hello {{ name }}!
  </h1>
</div>
    <div class=main-row>

        <div class="col-3 menu">
          <ul>
            <li>Login</li>
            <li>Download File</li>
            <li>Upload File</li>
            <li>Exit</li>
          </ul>
        </div>

        <div class="col-9 context-area">
        </div>
    </div>
    <div class=footer></div>
  </body>
</html>

This code is working until:

server.utilize(StaticFilesHandler::new("www/"));

I want to send the data $name = "Nickel" and render the index.html.

How do I render the whole www/ directory so that after visiting 127.0.0.1:6767/login the whole page will be there (JS, CSS and images) and not just plaintext?

I can provide the CSS file on request.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Daedalus
  • 295
  • 2
  • 17

1 Answers1

0

You have to re-utilize the www directory after the GET request:

server.get("/login", tmpl_handler);
server.utilize(StaticFilesHandler::new("www/"));

is working... even if it doesn't seem really good.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Daedalus
  • 295
  • 2
  • 17