So let's switch over to our VS Code editor, and let's use Spin to create a new serverless app, and along the way, we'll talk about both how this serverless environment works, and some of the cool nuances of the way that TypeScript works in this environment.
All right, here we are in VS Code. So the first thing to do would be to install Spin. You can head over to developer.fermion.com and kinda click your way into the Quick Start Guide and see how to quickly install and get up and running with Spin. I happen to just use Curl Bash to install it, so I've got it running here locally.
We're gonna create a new TypeScript serverless app. So just to quickly kind of see what all is supported, we can run Spin Templates List and see sort of all the different starter templates. Some do Redis with languages like Go or Rust, some do HTTP, Swift, Python. We're looking for this one here, HTTP TS, to run TypeScript applications. So let's create a new, spin new HTTP-TS. We're gonna create one called Congress, and we're gonna go ahead and accept the default, which basically just skips the walkthrough where it prompts you for a description and the default route and so on, and just chooses those for us.
So we can see it created an application for us. This should look very familiar to you. There's the package.json, the HTTPS-config. But there is this new file called spin.toml. We'll just quickly glance at that. This is the file that explains to spin how this application should be constructed as a WebAssembly serverless application. We can see in here in the component, this is the thing we're gonna be writing. This is gonna have a default route that'll listen on any, on anything. It'll use NPM to run its build, and the binary that will, the wasm binary will be called target slash congress.wasm. We're not gonna spend too much time in that file there. Instead, we're gonna get straight to the code. Let's take a look at index.ts. This is what a serverless event handler looks like. First, I'm gonna go in here and cd into the congress directory and run npm install and let that install this first library here in the code. It's the fermionspin SDK, which gives us our HTTP request and response and then we can take a look while that's running at our actual request handler. So it's gonna take an HTTP request, it's gonna return a promise for an HTTP response and all it's gonna do is send this hello from ts SDK. So there we've got kind of our scaffolded out original function, so let's go ahead and build that. Spin build is gonna package this up and turn it into a WebAssembly binary for us and then when that's done, we can do a spin up and have it run a version on localhost for us, so it's listening here on port 3000, we can use curl and quickly issue a request and see that that worked. So let's just change this to something very trivial, ts congress, save that and then once more, we'll do this build process.
Comments