0

I'm using a ResourceHandler to provide javascript files when I go on my web server. The thing is now I would like to provide the exact same javascript but accepting /{id} in my url so I can use the {id} when my website does POST request inside the handleRequest method. I tried with a pathTemplate but when I try to access my website it says that it can't find one of my js files (it works if I do not use the pathTemplate).

What can I do to make it works?

Nicolas Sagala Beaucage
  • 1,229
  • 1
  • 11
  • 23

1 Answers1

2

You can define two routes, one for your controller (to receive the post data) and one for serving your exact javascript file.

A more standard solution is to have a route dedicated to serve all the assets (including your javascript app). For this, look at the following answer: Routing template format for undertow

Undertow.builder().addHttpListener(8080, "0.0.0.0")
  .setHandler(Handlers.path()

    // Controllers
    .addPrefixPath("/controller", Handlers.routing()
        .post("/{id}", exchange -> {
          String id = exchange.getQueryParameters().get("id").getFirst();
        }))

    // Serve your file, preserving any route information
    .addPrefixPath("/app.js",  exchange -> {
        Path p = Paths.get("/path/to/app.js");
        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/javascript");
        exchange.getResponseSender().send(
          ByteBuffer.wrap(Files.readAllBytes(p))
      )})
).build().start();

With this example, your controller will be available at the route /controller/{id} and your javascript file will be served directly.

Note that this way of serving file is not optimal, it works if files are not too large. For a better way of serving files, one can play with Undertow's PathResource and PathResourceManager classes.

ant1g
  • 969
  • 9
  • 13
  • I may have missaid what I want I would have a path mywebsite.com/{id] which is the "entry". My clients will enter their own id that I will provide to them and then catch it in my javascript to add it in my post request. Using controller/{id} it will be working. I just need the first part and that's what I don't find how to do it – Nicolas Sagala Beaucage Oct 19 '17 at 14:19
  • From my example, if you make an http request to "/static/1234" it will remain in the url so you can grab it with your javascript logic. I still do not know how you are initiating the download of your javascript file (from some plain index.html?) – ant1g Oct 19 '17 at 20:32
  • I just tried and it seems to try to access "/static/1234" subfolder. Yes I have a html file and I use aurelia. So I initiate the download of my javascript file by script tag inside my index.html. For now instead of doing /1234 I'm doing #1234 and it works but I would rather do /1234 than #1234. – Nicolas Sagala Beaucage Oct 19 '17 at 21:03
  • That makes sense, the resource handler will try to locate resources in a specific folder and give a 404 if not found... Perhaps a query param? /static?id=1234 – ant1g Oct 20 '17 at 08:16
  • Alright thanks for all your help! I'll use # for now an when I have time I'll extends the ResourceHandler and make it works! – Nicolas Sagala Beaucage Oct 20 '17 at 15:28