Skip to content

Instantly share code, notes, and snippets.

@kjunichi
Created October 2, 2012 04:55
Show Gist options
  • Save kjunichi/3816240 to your computer and use it in GitHub Desktop.
Save kjunichi/3816240 to your computer and use it in GitHub Desktop.
How to use server side javascripts on Jsdo.it
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment