How do I deploy HTML5 APP to the local server

I created an HTML5 app using Construct3.
how can I deploye locally with rust
HTML

C:.
│   data.json
│   **index.html**
│   style.css
│   workermain.js
└───scripts
        main.js
        supportcheck.js

web server unable to process javascript request in index.html.
I read the content in the xxx.js file and return it directly to the client. <-- it doesn't work

    let listener = TcpListener::bind("127.0.0.1:80").unwrap();

    for stream in listener.incoming() {
        let stream = stream.unwrap();

        handle_connection(stream);
    }

fn handle_connection(mut stream: TcpStream) {
   let mut buffer = [0; 1024];
   stream.read(&mut buffer).unwrap();

    let (status_line, filename, contents) = if buffer.starts_with(b"GET / HTTP/1.1\r\n") {
        ("HTTP/1.1 200 OK", "index.html", "")
  }else if buffer.starts_with(b"GET /scripts/main.js HTTP/1.1\r\n") {
        ("HTTP/1.1 200 OK", "scripts/main.js", "")
}
   

    let contents = format!(
        "{}\n{}",
        fs::read_to_string(filename).unwrap_or_else(|e| format!("{}", e)),
        contents
    );
    let response = format!(
        "{}\r\nContent-Length: {}\r\n\r\n{}",
        status_line,
        contents.len(),
        contents
    );

    stream.write(response.as_bytes()).unwrap();
    stream.flush().unwrap();

It seems you’re manually implementing http over tcp which I would advise against for various reasons. It’s better to use an established http framework which would also correctly handle mime types for you, which seems to be the problem here (I think).

1 Like

Assuming you're trying to write the http server yourself for some reason, instead of using a library, you should first check that your content is correct, and that your server is returning everything that's needed, with some other server.

For example, I was verifying the same guess as @MoAlyousef that I had that you need to have a Content-Type: application/javascript, but it turns out you don't: but you do need to close a script tag - otherwise you get no error messages in the console or network tab, but the script doesn't run!

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.