DEV Community

Cover image for ๐Ÿš€ Five-Minute Backend Deployment Made Simple: Revolutionize Your Workflow
Rakshit Raj
Rakshit Raj

Posted on

๐Ÿš€ Five-Minute Backend Deployment Made Simple: Revolutionize Your Workflow

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
Enter fullscreen mode Exit fullscreen mode

MacOS

To install the Space CLI on MacOS, open a Terminal session and enter:

curl -fsSL https://get.deta.dev/space-cli.sh | sh
Enter fullscreen mode Exit fullscreen mode

Linux

To install the Space CLI on Linux, open a Terminal and enter:

curl -fsSL https://deta.space/assets/space-cli.sh | sh
Enter fullscreen mode Exit fullscreen mode

Create an account

Click here to create an account

Signup page of deta space

Skipping to Post Sing Up part, After creating your account, go to your dashboard (Builder UI)

Builder UI Page after

Search for "Settings" in the search box present at bottom of the page.

Setting Modal Deta Space

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
Enter fullscreen mode Exit fullscreen mode

This command will ask for an โ€˜access tokenโ€™ which you generated above to authenticate your CLI.

? Enter access token >
Enter fullscreen mode Exit fullscreen mode

Go to your project directory and Open an Terminal and type:

space new
Enter fullscreen mode Exit fullscreen mode

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.

Terminal View for creating a project

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Execute following command in common directory in terminal:

space dev
Enter fullscreen mode Exit fullscreen mode

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 testand docspaths:

Inside .spaceignore

test
docs
Enter fullscreen mode Exit fullscreen mode

Setting Environment Variables (.env)

Environment Variables can be created/managed via 2 ways:

  1. After pushing the code manually creating or changing the env inside the project settings from the Dashboard.

  2. 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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

After executing the CLI command it will start its all the configuration within the Spacefile resulting in response mentioned as below:

Pushing Files to Deta Space

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
Enter fullscreen mode Exit fullscreen mode

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

Deployed Project Builder

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)

Collapse
 
aquib399 profile image
Aquib Alam

hey!! i must say you're genius