LoginSignup
2
2

More than 5 years have passed since last update.

h2o web serverのmrubyを使ってみる

Last updated at Posted at 2018-04-16

spa(single page application)のindex.htmlをh2oで配布する際どうすりゃいいのか。

vueとかのspaだと、vue-routerとかつかってルーティングするため、
http://xxx.com/aaa/bbb/cccでアクセスしたときも、index.htmlを返却して、その中でjsを解釈してルーティングをする。

h2oでindex.htmlを配布するさい、どうすりゃいいのかわからなかった。
nginxだとtry_filesとかいうので対応できるみたい。

h2oはmrubyでフック処理をかけるので、それで簡単に対応できたけど、本番環境だと、他にいろいろ考慮しなければいけない感じがする。

runitで動かしてるのでファイル指定などは相対パスにしてる。

listen:
  port: 8000

hosts:
  "0.0.0.0":
    paths:
      /:
        mruby.handler-file: './forspa.rb'
        file.dir: /tmp/somewhere

./forspa.rbの内容。

path = "/tmp/somewhere"
cont = File.open("#{path}/index.html").read()
lambda do |env|
  unless File.exist?("#{path}/" + env["PATH_INFO"])
    return [200, {'content-type' => 'text/html'}, [cont]]
  end
  return [399, {}, []]
end
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2