🚀 Five-Minute Backend Deployment Made Simple: Revolutionize Your Workflow
Tags: backenddevelopment, node, cloud, tutorial
Published: 2023-09-04 00:11:43

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](https://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 ](https://deta.space/signup) to create an account

![Signup page of deta space](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jclc9av7ycorys2duyk0.png)

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


![Builder UI Page after ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zyb54c6xfie419fgskl2.png)

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


![Setting Modal Deta Space](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q8wvd8lrpuy94ue9y3vn.png)

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.

![Terminal View for creating a project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uviuw1zcprlly10hz0l2.png)

> CLI will automatically detect the engine environment (i.e. nodejs16, next, python)

Know More about Runtime on [Official Docs](https://deta.space/docs/en/build/fundamentals/the-space-runtime)

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:

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"
```

> **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](https://deta.space/docs/en/build/reference/spacefile).

---
## 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:

![Pushing Files to Deta Space](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cumaurskv7w4tl4op1mm.png)

> **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](https://deta.space/docs/en/build/quick-starts/node) Quick Start Guide 

![Deployed Project Builder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kr1ghxflntc6bcnxbtog.png)

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](https://deta.space/docs/en/build/quick-starts) 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`.