Java Spark Framework: Route to a specific static file

天大地大妈咪最大 提交于 2019-12-25 08:53:12

问题


Is there a simple way of setting up routes to a specific static file using the Java Spark Framework?

I'm setting up an Angular (1.5) app to be served from a Java Spark server. I've set up the general way of serving static files:

staticFiles.location("/public");

The app will have several angular routes, e.g. /start, /config, /live, etc.

Angular will take care of the routing when the index.html and the js-files have been loaded. But I want support for loading the application directly on the /start, /config, /live pages. To accomplish this I need to serve index.html from all these routes.

With Python Flask, you can do something like this:

@app.route('/live')
@app.route('/start')
@app.route('/config')
def index():
    return app.send_static_file('index.html')

I can't find anything that looks like this in Spark.

In the long run I'll probably serve the static files from NGINX, where you can easily do this. But for now it would be really nice to be able to do this in Spark.

Anyone? :)


回答1:


You can implement this in such way:

And now map routes with index.html:



来源:https://stackoverflow.com/questions/39489986/java-spark-framework-route-to-a-specific-static-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!