LoginSignup
10
9

More than 5 years have passed since last update.

Emacs で Web アプリつくろうぜ!

Last updated at Posted at 2013-07-17

某社で新卒3年目までを対象にエンジニアを競わせるイベントがあるそうなので、久しく作ってない動的なウェブサイト作成のリハビリをしたいなーと色々ウェブアプリケーションフレームワークを探してるところです。

その過程で、以前から気になっていた elnode をちょっと試して見ました。途中でグングンモチベーションが下がって、Hello World で終わってます。

準備

emacs 24 で以下のどちらかで elnode をインストール:

  1. M-x list-packageelnode インストール
  2. M-x package-install<RET>elnode (最近 list-package を実行した場合に限る)

動かしてみる

簡単なアプリの作成 (app.el):

app.el
(package-initialize)

;; デフォルトで起動するサーバを無効化
(setq elnode-init-port nil)

;; ポート番号
(defvar myapp-port 8000)

(defun myapp-helloworld-handler (httpcon)
  (elnode-http-start httpcon 200 '("Content-Type" . "text/html"))
  (let* ((host (elnode-http-header httpcon "Host"))
         (title (format "This is %s" host)))
    (elnode-http-return httpcon
                        (format
                         "<h1>%s</h1><p>Hello, world!! %s" title title))))

(elnode-start 'myapp-helloworld-handler :port myapp-port)

(message (format "access localhost:%d" myapp-port))

端末から elnode を立ち上げる:

端末
emacs -q -l app.el

これで完了。ブラウザで http://localhost:8000/ にアクセスすると Hallo world が見られる。

停止は、M-x elnode-stop<RET>8000。emacs が立ち上がってる端末で C-c、emacs 上で C-xC-c でもよさそう。

サンプル を見るとウェブアプリに大切な Hosts ヘッダやパスを使ったルーティングしてくれるハンドラーや、静的なHTMLを配信するためのハンドラーなどがはじめから入っている様子。

雑感

  • 「今開いてるファイルを起点に HTTP サーバ立てて、ブラウザからHTMLで書かれたドキュメント見る」って使い方使えそう。
  • ウェブアプリとしては。。。「あえて選択するほどの理由が見つからない」って事がくらいしか、選択する理由がないかなぁという感じ。
10
9
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
10
9