In the ever-evolving landscape of web development, one challenge that many developers face is deploying a backend for their projects without breaking the bank. While there are numerous hosting options available, finding a cost-effective solution that doesn't compromise on performance and security can be a daunting task.
In this article, i will explain and share my experience with numerous hosting providers and why i choose this one. Also about the struggles developers encounter when trying to deploy a backend for free and provide insights into some viable options to overcome these hurdles.
Deploying Backend using Deta Space
Deta Space, which is a cloud-based platform provided by Deta, a company known for its serverless tools and services. Deta Space offers developers a space to build, deploy, and host web applications and APIs as Deployment or Stagging before pushing for Production using serverless architecture.
Without Wasting time initializing the process...
Installing the Space CLI
Windows
To install the Space CLI on Windows, open PowerShell and enter:
iwr https://deta.space/assets/space-cli.ps1 -useb | iex
MacOS
To install the Space CLI on MacOS, open a Terminal session and enter:
curl -fsSL https://get.deta.dev/space-cli.sh | sh
Linux
To install the Space CLI on Linux, open a Terminal and enter:
curl -fsSL https://deta.space/assets/space-cli.sh | sh
Create an account
Click here to create an account
Skipping to Post Sing Up part, After creating your account, go to your dashboard (Builder UI)
Search for "Settings" in the search box present at bottom of the page.
Click on Generate Access Token and copy the access token
Account Setup Complete moving to next step...Project Setup
Project Setup
Youβll need to log in to Deta Space. From a terminal type:
space login
This command will ask for an βaccess tokenβ which you generated above to authenticate your CLI.
? Enter access token >
Go to your project directory and Open an Terminal and type:
space new
This command is like git init
you will be prompted to enter a name for your project. The CLI will display a generated configuration for the app and prompt you to confirm.
CLI will automatically detect the engine environment (i.e. nodejs16, next, python)
Know More about Runtime on Official Docs
Once confirmed, the project will be created along with a Spacefile. The Spacefile contains the configuration for your Micro and a .space directory that stores project information and links it to your project.
Inside Spacefile
If CLI detects your backend needs an engine (environment) as nodejs, **Spacefile **will consist of following after initializing by default, if not paste it manually.
# Spacefile Docs: https://go.deta.dev/docs/spacefile/v0
v: 0
micros:
- name: nodejs-app
src: .
engine: nodejs16
primary: true
NOTE: For now, the Spacefile with the generated configuration only works when there an
index.js
file at the root directory of the microβs source code else you will have to explicitly define what command should be executed to start your app with the run command. Shown below
# Spacefile Docs: https://go.deta.dev/docs/spacefile/v0
v: 0
micros:
- name: nodejs-app
src: .
engine: nodejs16
primary: true
run: node app.js
Deploying Locally
Add dev
: commands inside Spacefile
# Spacefile Docs: https://go.deta.dev/docs/spacefile/v0
v: 0
micros:
- name: nodejs-app
src: .
engine: nodejs16
primary: true
dev: nodemon index.js
Execute following command in common directory in terminal:
space dev
Restricting Uploads
Although .env
files are not been uploaded by default, but to exclude certain files and directories from being uploaded during space push
, use the .spaceignore
file. It acts similar to a .gitignore
file, where each line in the file specifies a pattern. All files and directories matching the patterns will be excluded from push.
For example, using space push
with the following .spaceignore
file will ignore the test
and docs
paths:
Inside .spaceignore
test
docs
Setting Environment Variables (.env)
Environment Variables can be created/managed via 2 ways:
After pushing the code manually creating or changing the env inside the project settings from the Dashboard.
While pushing your code from CLI, Spacefile can be edited in such manner:
micros:
- name: nodejs-app
src: .
engine: nodejs16
primary: true
presets:
env:
- name: APIkey
description: Secret API key only available to this Micro
default: "0bd5f0808f412635548fd67472398aab2bcffdbf"
- name: PORT
description: Listening PORT for my backend
default: "8080"
Common Error: People usually use PORT as 3000 which might be already reserved for frontend or some static micro services. Recommended Use
PORT: 8080
Deployment Visibility
This service is capable of Authentication too, but you can use them as per your need. For example, if you need your project to be visible to all, edit and add public: true
inside Spacefile.
# Spacefile Docs: https://go.deta.dev/docs/spacefile/v0
v: 0
micros:
- name: nodejs-app
src: .
engine: nodejs16
primary: true
public: true
Or using Custom Authentication API Key via generating manually from dashboard or while push the code, fir while change as shown below to your Spacefile:
micros:
- name: nodejs-app
src: .
engine: nodejs16
primary: true
presets:
api_keys: true
Use the api_keys
preset to enable the use of API keys to access a private routes of a Micro.
The user of your app will be able to generate API keys in the app settings and can use them in requests to a Micro using the X-Space-App-Key
HTTP header.
NOTE: These are some basics about Sapcefile creation and project management while push. Spacefile is capable of numerous advanced concepts like event scheduling which can be set manually.
For More Information regarding Spacefile checkout it official docs.
Pushing n Serving Files
Finalizing the project with a proper Spacefile
ready, open the terminal current directory and type:
space push
After executing the CLI command it will start its all the configuration within the Spacefile
resulting in response mentioned as below:
NOTE: Everytime if there is some changes in the file just use the same command
space push
, this will rebuild the project files as per configuration.
For visualization of deployed project, open terminal and type:
space open
Will redirect to your dashboard where you can set the .env and check the live logs.
Conclusion
Congratulations! π You have successfully built, deployed and got your first Node.js app on Space. π
This article is heavily inspired by Nodejs Quick Start Guide
Similarly, there are many more options such as fastapi, django, go, rust, svelte, sveltekit, nextjs, nuxtjs and many more, even you can deploy costume way. For trying each and every visit official quick start guide.
NOTE: Remember that this article is written on 4th September 2023, and till the date Deta Space has no pricing plans yet, thus resulting as a useful technology of personal cloud for completely free with crazy customization and serverless deployment.
I have personally used there service for more than 6 months straight without any deployment roadblocks or issue. It works fine.
Follow for more such technologies related to Servers
, Cloud Computing
, and FullStack Development
.
Top comments (1)
hey!! i must say you're genius