- /
- Build
- /
- Guides
- /
- Build A To Do App
Build a To Do App
This guide will teach you how to build your first internet application on Deta Space. It assumes zero prior knowledge about Space itself, and is meant to teach through practice. If you want to read more in depth about the concepts behind Space, see Intro to Space apps.
The guide has 5 sections:
- Overview is a quick overview of Space and what weâll build
- Prerequisites are what you need before you can start building
- Projects, Builder and the Space Runtime will set you up and briefly touch on a few concepts
- Coding the app will teach you how to code an app for Space
- Running the app will teach you how to run your app on Space, and emulate it locally
Overview
What is Deta Space?
Deta Space is a personal computer that lives in the internet, or âpersonal cloudâ. A personal cloud is a new approach to cloud computing, making it a lot simpler for developers to create and share software on the internet. Itâs also a completely new way of using internet software that puts you, as a user, in the driverâs seat. If youâre curious, you can read more here.
Whatâs important to realize about the personal cloud is that every user of an app gets their own copy, with their own resources to power it. As you build the app, youâll see you donât think about auth or other users â you assume thereâs just you. But once youâve written your app, youâll be able to publish it to just about anyone, if you want.
Donât be afraid if this sounds new â Space supports most common programming languages and frameworks. Your code will resemble a typical web application. If anything, youâll probably end up writing less code than you would otherwise. Thatâs because Space itself gives you a lot out of the box.
What Are We Building?
In this guide, weâll show you how to build a simple to do app on Space. You can see what weâll be building by installing the âTo Do Appâ (needs link) from Deta Discovery and trying it out yourself.
The app consists of two main parts:
- a User Interface made from a static frontend built with HTML, JS and CSS
- a Node.js server which intermediates between User Interface and a database (which comes with Space)
Prerequisites
This guide assumes you have a basic understanding of how a web app works. It also uses JavaScript, but you should be able to follow if youâre used to another programming language.
Deta Account
Since we are building an app for Deta Space, you will need a Deta account. You can signup for free here if you havenât already (make sure Developer Mode is enabled).
Space CLI
An essential part of building a Space app is the Space command line interface (CLI). We will use the CLI in this guide. Youâll also need it in the future to build your own Space apps. Make sure you install and log in to the CLI before starting. Read more about that here: Setup the Space CLI guide.
Node.js
In addition to the Space CLI youâll also need the following installed on your machine:
- Node.js â„18
- NPM
The full code for the app that we are building is on GitHub: https://github.com/deta/todo-app. You can clone it if you want, but you wonât need to, to complete the guide.
Projects, Builder and the Space Runtime
Any Space app starts as a Project, which provides tools for you to build, manage, and debug a Space app. Projects are created in Builder, which is a System application that comes pre-installed in your Space, if youâre a developer. With Builder, you can manage different projects that youâre working on.
To get started with our to do app, lets open a terminal on your development machine, create a new directory for our app called todo-app
, and then navigate into it:
Next weâll create a new project in Builder using the Space CLI. We can use the space new
command for this:
The CLI will ask you for a Project name, weâll call ours todo-app
:
The CLI will now create your Project and link your local directory to it. You may have noticed a Spacefile
was created in the directory where we ran the space new
command. Deta Space has its own runtime, the Space Runtime, which is the âenvironmentâ where your app runs. And the Spacefile
is used to tell the Space Runtime how to run your app, but weâll explore it more as we go.
The runable component within the Space Runtime is a Micro, a serverless compute unit that can be configured to serve static files, run a server or handle other types of events. Micros can be written in almost any programming language or framework. Our Quick Starters have more information.
Letâs get back to our app.
Coding the app
A Space app can combine up to five different Micros, but for this app we will need two:
- a static Micro for our frontend
- a Node.js Micro for our backend
Deta Space also offers two built-in data storage primitives: Base, a NoSQL database and Drive, for storing files. Weâll use Base in our app, which weâll get to in a bit.
Frontend
Letâs start with the our frontend. Weâll create a new directory in our Project and navigate into it:
Then we can create a simple index.html
file in the new directory. This will serve as the entry point for our app.
index.html
In index.html
you can see we are importing a JavaScript file called main.js
. Letâs create this file as well.
main.js
We donât have to go through this file in detail, but whatâs important to know is that it talks with our backend (yet to be created) using an /api
endpoint to create and fetch to do items.
For some simple styling we can optionally create a CSS file style.css
.
style.css
Backend
Our next step will be to create the backend API, which will receive instructions from our frontend and talk to our database. For this letâs create another Micro in the backend
directory:
Our API will be built using a Node.js server. Weâll use Express as a framework, so letâs create a new NPM project and install the express
and deta
packages (more on that in a bit):
For our simple API weâll create a index.mjs
file that will contain our API routes and start the server.
index.mjs
There a few important things happening in this file, letâs go through each one:
Starting the server
At the bottom of the file, youâll see that we are starting the HTTP server using the PORT
environment variable. The Space Runtime will route HTTP requests to our Micro on the port set in this variable. Since our app talks over HTTP, this is necessary.
Connecting to Deta Base
At the top of the file we are importing the express
package and also the deta
package. express
will run our web server, while deta
is an SDK which you can use to talk with Spaceâs built-in database, Base.
You can see that we are instantiating the SDK with Deta()
and then referencing a Base called todos
using deta.Base('todos')
in our db
variable. This is all that is needed to setup and connect our server to a fully functional database.
If you examine the four handler functions in index.mjs
, youâll see how GET
, POST
, PUT
and DELETE
requests are translated into reading, creating, updating and deleting to dos from our todos
database. We just use a few simple methods the Deta SDK offers on our db
object.
index.mjs
If youâre curious about what else Deta Base has to offer, you can read more here.
One more point weâd like to mention. As we referenced earlier, the notion of multiple users is completely abstracted away in our code. With Deta Spaceâs authentication, there is no auth code or logic dealing with multiple users managing their own to dos. Nonetheless, Space will still let us release our app to many users around the world, with this simple setup, which weâll get to later.
Configuring the app using the Spacefile
Now that we have both Micros setup, the last step is to connect them together into a single app. For that weâll shift our attention to the **Spacefile**
that was created in the beginning. If you open it, youâll see the file should be almost completely empty:
It only includes a link to the Spacefile
docs and the version field.
Letâs change that and add a new field for our Micros called micros
and add the frontend Micro to it:
You can see that the Micro has a few different fields set, weâll explain them:
name
: this identifies your Micro and can be anything you like, weâll call oursclient
src
: this is the source of your Micro and it should be set the the directory that contains your Micro, in our casefrontend
engine
: this tells the Space runtime what type of Micro you are using. Since our Micro is a static frontend we set it tostatic
serve
: this tells the Space runtime which directory contains the static files that it should serve. Since our simple frontend has all the files in the Microâs source directory we can just set it to the current directory using a dot.
This would already be enough for Space to run our frontend as a full Space app. Our frontend also needs a backend though, so letâs set that up as well.
Weâll just add another Micro to the micros
field in our Spacefile
:
The fields for this Micro are as follows:
name
: weâll call oursapi
src
: the source of our server is in thebackend
directoryengine
: Since we are using Node.js for our server, letâs usenodejs16
run
: this tells the Space runtime what command to use to run the Micro. Since ourindex.mjs
file starts our server weâll just run the file withnode
dev
: this tells the Space runtime what command to use for development. We use the same command as forrun
but with thewatch
flag enabled, which allows node to automatically reload our server when we change something
Read more about the complete list of Spacefile
options here.
Application Routing
Now that we have our two Micros configured, you might be wondering how Space actually runs them, and how they are able to handle incoming requests across different routes.
Space requires you to choose a Primary Micro, as you need at least one Micro to handle all incoming requests. This Micro will serve as the fallback, if no other Micro is handling a request. Your Primary Micro will also be available on the root (/
) of your app.
To do this we can use the primary
field of a Micro in the Spacefile
. For our to do app we want our frontend to be the Primary Micro, so weâll add the field to it:
Every other Micro that is not the Primary Micro will be available under a specific route, relative to the root /
. In our case, since our backend Micro is called api
, the Micro will be available on /api
.
You can see this if you take a look at our main.js
file in the frontend
directory, where our frontend is talking to our backend via a simple fetch call to the /api
route:
Meanwhile, in our backend server in index.mjs
, all the handlers will be relative to the /api
route. As we send the GET
request to the /api
route of our app, it will trigger the app.get('/', async (req, res) => {})
backendâs handler.
Running the app
Locally
Once we have our app fully configured using the Spacefile
, we can run it locally to test if everything works. For this weâll use the space dev
command. It will start both of our Micros and emulate the routing of our entire Space app.
Letâs try it by running space dev
:
To access your app, open a browser window and visit http://localhost:4200/
. You should see our fully functional to do app!
Try adding to dos and marking existing todos as complete. Everything should work â even when you reload the page your to dos should remain the same.
Behind the scenes space dev
setup a new Base for our to dos in our Builder Project. We can view the raw data in the Base with a UI in Builder. To open our Project there, we can just run space open
and a new browser window should appear with the Builder interface open:
Navigate to the âDevelopâ page and then the âDataâ tab to view your Base:
You can use the Base UI to edit your data and the changes should show up in your local app after reloading the page.
On Space
Now itâs time to put our app live on the internet in Deta Space. To do this we can simply run another command with the Space CLI, space push
:
space push
will take all of the source code, the configuration from the Spacefile
, and send it to the build pipeline to create a Revision. This revision reflects an executable copy of your app at a point in time.
After the revision is created, the CLI will also create a new Builder Instance than runs the revision. This instance is a live version of your app on the internet. It has all the same features as any other Space app, the only difference is that it is connected to your Builder Project.
The CLI will print a URL which you can use to access and use your Builder Instance. The instance will also show up on your Space Horizon.
Congrats, you have just built your first Space app! đ
đĄ You will notice that your instance already contains the to dos that you created through the local app. This is because your Builder instance shares its data with your Builder Project (including local dev).
Important Takeaway
One very important detail youâll notice is that your instance is only accessible to you. It is protected behind Deta Auth: if youâre not logged in to your Deta account, or in incognito, you canât access it. This is fundamental to how Space works â apps are for you, by default.
If you are building an app that needs some or all parts to be public, like a website, you can use public routes to make specific routes or entire Micros public.
Wrapping up
Congratulations, you have built your very own Space app!
You have learned how to:
- create a new Builder project
- create and configure Micros
- use Deta Base to store and retrieve and view data
- run your app both locally and on Space
Good job! We hope you now feel like you have a decent grasp of how a Space app works.
Going Beyond
Extending your To Do App
If you have extra time or have more motivation, here are some ideas on how to improve this to do app and take it even further:
- Add another scheduled action that creates a recurring to do item
- Add another Base that keeps track of different task lists
- Use Deta Drive to allow the user to upload files to a to do item
- Create publicly shareable to dos with public routes
If you want to dive deeper into the concepts behind Space and all the features it has to offer, head over to our Fundamentals, Guides, or Quick Starts.
Publishing
Space is based on the personal cloud, where apps and data are personal. But once youâve built something personal, the personal cloud also has a powerful publishing model where you can make your app available to almost anyone in the world with an internet connection.
Read more about publishing on Space here.