nickel-org / nickel.rs

An expressjs inspired web framework for Rust

Home Page:http://nickel-org.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Guidance Serving ReactJS SPA

Phillips126 opened this issue · comments

I've been developing a ReactJS frontend that uses a Rust backend. The Rust backend essentially queries a binary, parses the data and serves an API for the React frontend to hit. So far, Nickel.rs has been awesome to work with.

Now, I've built my ReactJS application and want Nickel.rs to also serve the static html.
I've found that the following code works, however, I am unsure how to handle the SPA routing portion.

let mut server = Nickel::new();

// Serve the compiled ReactJS application at "/"
server.mount("/", StaticFilesHandler::new("app/build/"));

// API endpoints for ReactJS to fetch
server.utilize(router! {

  get "/data/a" => |_, mut res| {
    res.set(MediaType::Json);
    json!(&a)
  }

  get "/data/b" => |_, mut res| {
    res.set(MediaType::Json);
    json!(&b)
  }

});

// Serve...
server.listen("0.0.0.0:6767").unwrap();

I handle the routing with React Router. With React Router, I have the following routes (simplified of course):

"/"    =>   "home" component
"/a"   =>   "a" component
"/b"   =>   "b" component

With the above static router if I navigate my browser to: localhost:6767/ then my application loads and I can navigate around inside the app as normal. However, if I were to type the explicit url: localhost:6767/a then I am met with a "Not Found".

Any guidance how I may be able to use Nickel.rs to work with a SPA?

I'm not familiar with ReactJS. localhost:6767/a would statically serve the file "app/build/a". Did you mean localhost:6767/data/a, which would call the middleware you defined?