Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Serverless application development is growing in popularity in no small part due to the appealing proposition to ābuild apps, not infrastructureā. This way of building apps provides many benefits including not having to manage server infrastructure, infinite scalability and not having to pay for idle server time, paying only when your service is inĀ use.
The Serverless platform further abstracts building serverless apps into a delightful developer experience that helps you get a cloud function-powered app up and runningĀ fast.
Cosmic JS provides a CMS API for teams to manage content for any new or existing application. The Cosmic JS Serverless Starter App gets you up and running with a Serverless app connected to the Cosmic JSĀ CMS.
In this tutorial Iām going to show you how to get started with the Cosmic JS Serverless Starter app in a matter of minutes. And as a bonus Iāll show you the steps to transform any Node.js app into an infinitely scalable Node.js Serverless App. Letās getĀ started.
TL;DR
Check out the Cosmic JS Serverless Starter onĀ GitHub
Install the Serverless Starter
To install the Cosmic JS Serverless Starter, run the following commands:
npm i -g cosmic-clicosmic logincosmic init serverless-starter
These commands do the following:1. Install the Cosmic CLI2. Login to your Cosmic JS Account (you may need to create a new Bucket, if so, itāll walk you through this š)3. Download the Serverless Starter and import the demo content to your selectedĀ Bucket.
Once itās downloaded, youāll be prompted to start the app with the following commands:
cd serverless-startercosmic develop
Your application should now be running at http://localhost:3000
Get Ready for LiftĀ Off
Now that we have our application running locally, letās get ready to launch it to the AWS cloud. First weāll need to connect our AWS credentials. To find these on your AWS account, Serverless has a great walk through on theirĀ website.
After youāve retrieved your AWS keys run the following commands:
export AWS_ACCESS_KEY_ID=your-aws-key-idexport AWS_SECRET_ACCESS_KEY=your-aws-secret-key
And now weāre all set to launch our Serverless Starter to AWS. Run the following command to login to the Serverless service and deploy ourĀ app:
npm i -g serverlessserverless loginCOSMIC_BUCKET=your-bucket-slug serverless deploy
The serverless deploy command does all the heavy-lifting for us. It performs some magic behind the scenes with our AWS account, then once our app is available, it will provide the endpoints to find the deployed app. Adding the environment variable COSMIC_BUCKET, connects our deployed app to our Cosmic JS Bucket via the Cosmic JS API. This enables us to manage content for our newly deployed serverless app from the Cosmic JS Admin Dashboard.
From the output above, you can see our available endpoints where our Serverless app is now deployed.
Make any Node.js App Serverless-Ready
So we ran a few commands and pushed the Cosmic JS Serverless Starter app to AWSā¦ cool. But letās go a bit deeper and take a look at how we transformed the Node Starter app and how we can convert most any Node.js app into a serverless app.
Edit serverless.ymlTo transform the Node Starter to a Serverless app, we had to create a serverless.yml file and set the config to handle dynamic routes. This is accomplished in the function > app > events area to indicate the dynamic http events. It looks likeĀ this:
Edit app.jsNext we needed to edit the entry point for the app, app.js file to export the main handler function. We also included the serverless-http node module to wrap our Express app for Serverless use. Hereās the app.js file converted to serverless:
In Conclusion
With a few edits, you can quickly transform a Node.js app into a serverless app. The Serverless platform abstracts the complexity of AWS account configuration to give you a delightful development experience. Add Cosmic JS-powered content and you have an infinitely scalable serverless app with content managed via the Cosmic JS API. No servers to manage, no CMS to maintain. Building apps, not infrastructure is theĀ future!
If you have any comments or questions about building serverless apps with Cosmic JS, reach out to us on Twitter and join the conversation onĀ Slack.
Build and Deploy a Serverless Node.js App powered by Cosmic JS was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.