2
0

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.

jsdo.itでサーバサイドJavaScript

Last updated at Posted at 2012-12-26

仕組み

Google App EngineでRingojsを動かし、そこからJsdo.it上のJavaScriptのソースを
GETして、evalする。やや危険な香りがする方式。

Google App Engineに設置するJavaScript

コードはこんな感じです。

exports.app = function(request) {
    var path = request.pathInfo.slice(1) || "index";
    // 1. resolve against actions
    if (typeof actions[path] === "function") {
        return actions[path](request);
    }
    // 2. resolve against public folder
    var resource = getResource("./public/" + path);
    if (resource.exists()) {
        return response.static(resource);
    }
    importPackage( java.net);
	importPackage( java.io);
	var sourceJs = "http://jsdo.it/[your accounts]/"+path+"/js";
	var url = new java.net.URL(sourceJs);
	try {
	var ucon = url.openConnection();
	var istream = ucon.getInputStream();
	var isr = new java.io.InputStreamReader(istream, "utf-8");
	var br = new java.io.BufferedReader(isr);

	var line = "";
	var buf = "";
	while ((line = br.readLine()) != null) {
		buf=buf+line+"\n";
	}
	// 読み込んだJavaScriptを実行する。
	var result="";
	// jsdo.it側でjsdoitServerFuncを定義する。
	eval(buf);
	result = jsdoitServerFunc(request);
	return result;
	} catch(e) {
		return response.notFound(e);
	}
    // 3. return 404 response
    return response.notFound(request.pathInfo);
}

Jsdo.itの作品のURLとのマッピング関連の仕掛け

web.xmlで任意の名前でリクエストを受けられるようにしておき、
PathInfoでjsdo.it側から読み込むソースのURLを決定します。

大事なこと忘れてた

RingoJSが必要です!

DLして、適当な場所で展開して動かします。

ringo-admin create --google-appengine /path/to/appdir

これでappdirが作成されるので、適当にEclipseに取り込んで、Google App Engineの
プロジェクトとして設定して、デプロイします。

参考ページ RingoJS runs fine on Google App Engine for Java

関連記事

2
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?