Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

AppengineJS: JavaScript Comes to GAE

DZone's Guide to

AppengineJS: JavaScript Comes to GAE

· Cloud Zone
Free Resource

MongoDB Atlas is a database as a service that makes it easy to deploy, manage, and scale MongoDB. So you can focus on innovation, not operations. Brought to you in partnership with MongoDB.

Although there's no JavaScript SDK made by Google for the App Engine platform, a new port of the GAE Python SDK to JavaScript called AppengineJS facilitates the construction of apps for GAE that are written in the language that all web developers are familiar with.  The port uses Rhino (the JVM implementation of JavaScript) on top of App Engine Java, but the API is based on App Engine Python because it's a scripting language like JS.

In a blog this week, George Moschovitis, one of the project developers, said that his project has recently garnered an intense amount of interest, and he had a couple of answers to readers' comments.  He said that if you already know Python or Java, you should stick with those SDKs, which are supported by Google.  The App Engine Python documentation should work effectively for the JavaScript SDK.  

The AppengineJS documentation explains that they use JavaScript coding conventions:

Python names like 'thisisa_name' are converted to JavaScript names like 'thisIsAName'. Moreover all delete() functions are renamed to .remove() functions to avoid collisions with the delete keyword (a DELETE() alias is also provided but it's uses is not recommended and may be deprecated in the future).

For the GAE Datastore, the the Python ext/db api is supported, but the API has been slightly modified to be more compatible with JavaScript:
var db = require("google/appengine/ext/db");

var Category = db.Model("Category", {
label: new db.StringProperty(),
category: new db.ReferenceProperty({referenceClass: Category})
});

var c = new Category({keyName: "news", label: News"});
c.put();
var key = ...
var c1 = Category.get(key);
var c2 = Category.getByKeyName("news");
var categories = Category.all().fetch(3);
...
var comments = Comment.all().ancestor(article).order("-created").withCursor(cursor).fetch(10);

Here's a sample of the AppengineJS documentation pertaining to the Blobstore:

Form:
var blobstore = require("google/appengine/api/blobstore");

exports.GET = function(env) {
return {data: {
uploadURL: blobstore.createUploadUrl("/test")
}}
}

<form action="{uploadURL}" method="POST" enctype="multipart/form-data">
<p>
<input type="file" name="file" />
</p>
<p>
<button type="submit">Upload</button>
</p>
</form>
Upload:
var blobstore = require("google/appengine/api/blobstore");

exports.GET = function(env) {
return {data: {
uploadURL: blobstore.createUploadUrl("/save")
}}
}
Save:
var blobstore = require("google/appengine/api/blobstore");

exports.POST = function(env) {
var blobs = blobstore.getUploadedBlobs(env);

return {
status : 303,
headers : {
"Location": "/serve?key=" + blobs.file.toString()
}
};
}
Serve:
var blobstore = require("google/appengine/api/blobstore");

exports.GET = function(env) {
var params = new Request(env).GET();
return blobstore.serve(params.key, env);
}

Also covered in the AppengineJS docs are URL Fetches, Images, Email, Memcache, Users, Task Queue, XMPP, Forms, Quota, and OAuth.  The library is still under development, but it's definitely usable right now.  Here is the breakdown of the level of Python API conversions:

    •    google/appengine/api/ memcache: 90% (usable)
    •    google/appengine/api/ urlfetch: 80% (usable)
    •    google/appengine/api/ mail: 80% (usable)
    •    google/appengine/api/ images: 60% (usable)
    •    google/appengine/api/ users: 90% (usable)
    •    google/appengine/api/labs/ taskqueue: 80% (usable)
    •    google/appengine/ext/ db: 80% (usable, expect minor API changes)
    •    google/appengine/ext/ db/forms: 60% (usable, expect API changes)
    •    google/appengine/api/ xmpp: 80% (usable)
    •    google/appengine/ext/ blobstore: 50% (usable)
    •    google/appengine/api/ oauth: 90% (not tested)

You can download AppengineJS at GitHub.  The SDK is part of the Nitro Ecosystem and tracks the latest developments in the CommonJS group.

MongoDB Atlas is the best way to run MongoDB on AWS — highly secure by default, highly available, and fully elastic. Get started free. Brought to you in partnership with MongoDB.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}