23
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Runkit で作るお手軽サーバレス Web API

Last updated at Posted at 2018-04-07

Runkitとは

Runkit は npm が使える Node.js 向け Playground です。それぞれのコード・スニペットを Notebook と呼び、npm からモジュールをインポートしたり、ある Notebook を別の Notebook へインポートしたりもできます。

今回は、この Runkit の機能のひとつである Endpoint という、Notebook を AWS Lambda ファンクションのように Web API として公開することができる機能について紹介します。

使い方

最もシンプルな使い方は

exports.endpoint = (req, resp) => {
  resp.end("Hello!");
}

こんな感じで exports.endpoint に リクエストとレスポンスを受け取るハンドラを定義するもの。

publishの横にあるendpointというリンクから、そのNotebookに割り当てられたEndpointのURLが分かります。
Selection_057.png

上のサンプルのEndpoint URLは https://untitled-pnq6ao8zpp2j.runkit.sh に割り当てられているので、そこへ httpie してみると

 $ http https://untitled-pnq6ao8zpp2j.runkit.sh/
HTTP/1.1 200 OK
connection: close
content-length: 6
date: Sat, 07 Apr 2018 04:31:54 GMT
runkit-rate-limit-remaining: 9989
strict-transport-security: max-age=31536000
x-powered-by: runkit.com

hello!

レスポンスに定義された文字列が返ってきました。

Runkitのヘルパを使う

上のシンプルなやり方は実際使うには不便です。なぜかというと、export.endpoint は内部では以下のように http.Serverrequest イベントコールバックに引き渡されるため、リクエストとして帰ってくるのが http.IncomingMessage というストリームだったりするので、リクエスト・ボディのシリアライズ処理などが面倒なのです。

server.on("request", (req, resp) => {
  resp.end("hello!");
});

なので、あらかじめ Runkit が用意している Endpoint 用のヘルパ Notebook をインポートしてもう少し便利につかいましょう。

const endpoint = require("@runkit/runkit/json-endpoint/1.0.0");

endpoint(exports, incomingData => {
  return incomingData.body;
})

これで incomingData としてシリアライズされたリクエストのデータを受け取れるようになりました。
上はパラメータとして受け取ったものをただエコーするだけの例ですが、これを再び httpie で叩いてみると

 $ http https://untitled-pnq6ao8zpp2j.runkit.sh name=justine
HTTP/1.1 200 OK
access-control-allow-origin: *
access-control-expose-headers: tonic-rate-limit-remaining
connection: close
content-length: 23
content-type: application/json; charset=utf-8
date: Sat, 07 Apr 2018 06:22:05 GMT
etag: W/"17-C5+qw1XJcXNTe59jonBwYQ"
runkit-rate-limit-remaining: 9956
strict-transport-security: max-age=31536000
vary: Accept-Encoding
x-powered-by: runkit.com

{
    "name": "justine"
}

リクエスト・ボディがしっかり受け取れているのが分かりますね。

Expressとの併用

const express = require("@runkit/runkit/express-endpoint/1.0.0");
const app = express(exports);

app.get("/:name", (req, res) => res.send(`hey ${req.params.name}`))

exports を Express に渡すことで、Express を使った Endpoint 実装もできます。

制限

Runkitは便利ですが、以下のような制限もあります。

  • Endpointで利用できるスキーマは HTTPS のみ
  • Betaのため非商用利用限定(商用利用したい場合は問い合わせが必要)
  • リクエストのタイムアウトは60秒で固定
  • エンドポイントにはレート・リミットあり
    • HTTPレスポンスに含まれている runkit-rate-limit-remaining というヘッダのカウントが0になったらおそらく終わり。なので時間か日単位でリセットされるまで9999回のリクエストまで可能。

とはいえ、ちょっと遊んだり個人で使うぶんには全く気にならなそうですね。

参考: Runkit Endpoint

23
17
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
23
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?