Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Server side Javascript in WebAssembly

Posted on Oct 23 The nice thing about writing a serverless functions is that you only have to write an event handler. Much of the boilerplate Code you'd write for a regular webserver-style app can be omitted.In this post, we'll write a server-side Javascript Function and then build it into a WebAssembly binary using the open source Spin tool. Our code will be less than a dozen lines long in total, so this is a concise introduction to WebAssembly and serverless functions that won't require you to spend a lot of time figuring out a code sample.To get started, we need to install Spin locally. Spin is a tool for building WebAssembly serverless functions, and then running them locally or deploying them to other runtimes. Spin will automatically download all of the supporting tools necessary for building Javascript apps into WebAssembly. You will need npm, though, if you don't already have it.Spin has four important commands:We'll use all four.To get started, let's build a new JS app. We're going to name it hello-dev.Above, we've used spin new to create a new http-js application. That means we are writing an HTTP event handler in JavaScript.Spin supports other handlers, like redis for listening on Redis pubsub queues.The hello-dev part is the name of our application. And the --accept-defaults flag tells Spin not to ask us interactive questions, but to instead just build us an application using the default settings.Once the command is done, we have a directory named hello-dev. This is what we see inside of it:Here's what those files are for:The first thing we need to do is change into the newly created hello-dev/ directory and run npm install to set up the local environment for us.That installs all of the core libraries we need for today's serverless function.Next up, open src/index.js in the editor of your choice. You'll see the following code:This is just a very basic JS function. Spin will look for a function named handleRequest(), so it is important that the function is named so. The request will be given an HTTP request object (here called request), and Spin will expect that we return an HTTP response object, which we do in the code above using a literal object notion that has status, headers, and body.If you're new to HTTP, status: 200 means success (404 means not found and 500 means server error, and there are several others). Anything in headers will be accessible to the web browser, but not displayed to the user. and body will be displayed to the user in their browser.Without making any edits to the code, we can build and test the application above.First, we build with spin build:Once the above command is done, you should see a new directory called target/, and inside that directory is a binary called hello-dev.wasm.Now we can use spin up to start up our new hello-dev.wasm serverless function.This starts a new local server listening on port 3000. So you can point your browser at that URL and see the result:That's our default serverless function! To stop the server, you can use CTRL-C.Next up, let's change the code. We'll make it a little shorter, and add a custom message.There are two things about the original code that we can do away with.So let's trim down the code and change the body to be something a little more personalized to this post:We're down to a six line serverless function. Once more, we can use spin build to build an updated WebAssembly binary, and then run spin up to start a local server.Once more pointing our browser to http://127.0.0.1:3000, we can see our new text.And there we have it! A Javascript serverless function in WebAssembly. If we wanted to deploy it to a server with a public URL, we could use spin deploy. By default, that will send it to Fermyon Cloud (using the free tier), and you'll be prompted to log in with GitHub. But you can also point Spin to other deployment platforms.Here's a quick example:Now I've got an app available on a public endpoint, and you can access it at https://hello-dev-ftv5h3fg.fermyon.app.Not everything that you find in the NPM catalog of libraries will work. Spin does not provide a 100% Node compatible runtime. But Spin does provide some things that Node.js and other JS runtimes do not:The best place to go from here is straight to the Spin Javascript Language Guide, where you can learn more about apps you can build.Templates let you quickly answer FAQs or store snippets for re-use.Spin is quite interesting, but is it practical too? Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Thomas Aribart - Oct 9 Archit Sharma - Oct 20 Odumosu Matthew - Sep 27 Matija Sosic - Oct 16 Once suspended, technosophos will not be able to comment or publish posts until their suspension is removed. Once unsuspended, technosophos will be able to comment and publish posts again. Once unpublished, all posts by technosophos will become hidden and only accessible to themselves. If technosophos is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Matt Butcher. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag technosophos: technosophos consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging technosophos will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Server side Javascript in WebAssembly

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×