DEV Community

Cover image for How do You Structure Your Code When Moving Your API from Express to Serverless Functions?

How do You Structure Your Code When Moving Your API from Express to Serverless Functions?

John Papa on March 20, 2019

There are a lot of articles showing how to use serverless functions for a variety of purposes. A lot of them cover how to get started, and they are...
Collapse
 
jon64digital profile image
jon64digital

Yes, please post more on this topic. I'm just learning to create my first node APIs using Express. Do you think I should try using Express with Azure functions or just ditch Express and go with a bare bones approach. It could end up being a fairly big API.

Collapse
 
juanitomint profile image
Juan Ignacio Borda

My 2cents on this, before code make the definition document with swagger editor (now open API) if you don't know learn this first as it will make your API more portable, then code it on express directly, once is up and running you can stay a migration to serverless endpoint by endpoint

Pd swagger editor can create servers and clients from a definition file automatically

Collapse
 
john_papa profile image
John Papa

Node and express are a fantastic and proven combination for apis. And it can do so much more. Serverless is a choice that helps scale up and down without the need for much server config

I would still lean on both as they are both excellent options.

If you need help designing an api with serverless I know the azure functions team is always very helpful with this as they help a lot of companies be successful.

Collapse
 
_mhoeger profile image
Marie H

+1 to Jaun's suggestion about starting with the definition document!!

To your question about starting with express and migrating vs. starting bare bones - I think it depends on your needs. Express has extensive middleware that can be very useful. Azure Functions has built-in functionality like proxies, easy integration to other services with triggers and bindings, and immediate deploy to prod. To echo John, both are great options.

As an aside, although this isn't an official package, you may be interested in checking out azure-functions-express on npm too!

Collapse
 
jon64digital profile image
jon64digital

Thanks everyone. I saw azure-functions-express but wasn't sure whether to go down that route or whether that was trying to crow bar something inappropriate into functions.

Collapse
 
dkamer profile image
David Joseph Kamer • Edited

I need to try azure so that's for the article.

I use firebase quite a bit, but I'm not happy with some of the choices they made like make node v6 the default... Like c'mon, v8 is the newest version you support, and I have explicitly put that in the config file so my clients have to come back for an upgrade later (maybe it has auto upgrade ^ support but I'm not going to try it with a piece of software that so consistently silently fails)?

That and the way they handle promises. That's a complicated one that would require a long, long tyraid about what not to do if you want to lint with your deploy tool and "optimize" your platform without creating unclear and seemingly random artifacts in production.

If Azure solves either of the problems, it might have new fanboy.

Collapse
 
john_papa profile image
John Papa

Hope you liked it!

Azure Functions offers Node 6 and Node 8,10. Depends on which version you use. To make it simple, if you create one today, you likely get 8 or 10. Here is a doc that explains all of this. docs.microsoft.com/en-us/azure/azu...

Collapse
 
danielcmorris profile image
danielcmorris

I currently have several clients on large MSSQL databases. I've been considering going this route,but I've also got a bunch of WebAPI servers written in C#. The idea of moving each API over, one by one, is appealing, but I'm not certain it's a good idea. Without changing from MSSQL, what would you recommend?

Collapse
 
_mhoeger profile image
Marie H • Edited

First, I have to apologize because I'm not too familiar with working with MS SQL databases!

Could you connect to your databases with a SqlConnection within your serverless function though? This documentation refers to some things to do to avoid connection limitations from SqlClient connections.

Another thing to keep in mind is that sometimes serverless functions scale too much for a database and hammer a non-scaling database with requests. To address those concerns, you can configure HTTP behavior and impose limits on function app scale out. Even if you constrain scale, you'll still receive the benefits of serverless :)

Collapse
 
papaponmx profile image
Jaime Rios

Thanks for sharing, I find this structure easy to understand and reminds me a lot about Angular.

Collapse
 
john_papa profile image
John Papa

Thanks, I’m glad it it’s helpful. I’m thinking of posting more content on this topic.

Collapse
 
g1llz profile image
Gilles Lima

Thanks for sharing!

Collapse
 
john_papa profile image
John Papa

Hi - no reason. const is my preferred when no changes exist.