Learn about new FL0 features and how developers are using FL0 to build their next big idea.
Nodejs
Postgres
Expressjs
DevOps
Tutorial
Hello Reader and welcome to the first article in the FL0 Engineering publication! Whether you are building a fintech, a health app or the next Neopets, there are some things almost EVERY startup needs…and we’re going to tackle those things in this article. We’ll begin by creating an API with Express, then we’ll connect a Postgres database and manage it with Sequelize. Finally we’ll deploy everything to a Docker container and make use of the great NodeJS and Postgres features in FL0.
The app we’ll build is going to be basic, but it will contain the essential components you need to extend it for your own project. It will include:
If all goes well, you should be up and running in less than half an hour!
You’ll find the completed codebase at this link on the building-deploying-30-mins branch.
This article will be fairly detailed with step-by-step instructions and code snippets where possible. However, it does expect you have…
And the following technologies installed on your machine…
If you have any questions on the setup or if you think there are any steps missing in this article, please drop a comment for us below!
Let’s start by setting up a local repository with the Express starter code. Open up a terminal window and run the following commands:
These commands do the following:
To get a basic Express server running, create a new folder called !!src!! and a file inside called !!index.js!! and paste in the following code:
Edit your !!package.json!! file and add the following !!start!! and !!start:dev!! commands:
Try out your app by running !!npm run start:dev!! and visiting http://localhost:3000 in your browser. See the words “Hello World”? Let’s keep going!
Before we move on to the next section, initialize your folder as a Git repo and save your changes:
The above commands achieve the following:
In this section we’ll create a local !!Postgres!! database and install Sequelize to manage it.
In the root of your codebase, create a file called !!docker-compose.yml!! and add the following content:
A Docker Compose file tells Docker what containers to spin up and how they should be configured. Our file contains a single service - !!db!!. It runs an image called !!postgres (version 14)!! and sets some configuration like the username and password for the database.
Our Docker Compose file is set up to create a database with username !!admin!!, password of !!admin!! and database name of !!my-startup-db!!. We will need to tell our app how to connect to the database, so let’s do that now. Create a file in the root of your repo called !!.env!! and add the following content:
The !!DATABASE_URL!! variable will be used when we set up Sequelize.
Before we continue, run the following command and verify your Postgres container starts correctly!
If you see a message like this, you’re good to go!
Sequelize is the most popular Javascript Object Relational Mapping )(ORM) framework. Other alternatives include TypeORM and Prisma. You can use any of these with FL0, but in this article we’ll choose Sequelize.
First let’s install Sequelize and the Postgres client for NodeJS, then use the Sequelize CLI tool to initialize our project:
The !!npx sequelize-cli init!! command bootstraps our project with a few folders and files:
Next, let’s alter the !!src/config/index.js!! file to point to our database. Replace its content with the following:
This tells Sequelize to read from the !!.env!! file we created earlier. To test our connection, alter the !!index.js!! file to look like this:
These changes import the !!sequelize!! object from our new !!src/models/index.js!! file and attempt to connect to the database.
Run !!npm run start:dev!! and make sure you can see a message that says “Connection has been established successfully”. If you see any errors:
Now is a good time to commit your changes!
Now that Sequelize is set up we can start populating the database with some tables and values. Let’s use the Sequelize CLI tool to create our first model. Run the following script in your Terminal from the !!src!! folder:
This will automatically generate !!src/models/product.js!! and a corresponding file in the !!migrations!! folder. Next, we’ll define some seed data to populate the Product table with some information:
Open the created file under the !!seeders!! folder and replace it with the following content:
The code above inserts three sample products for us to work with. To import the data, make sure your app is running with !!npm run start:dev!! and then in a new terminal window run:
Hopefully you see some output that looks like this!
Now that we have a database with some product records, it’s time to create an API to expose this information. Update !!src/index.js!! with the following content:
Notice the new !!/products!! route, and the !!Product!! object being imported from the Sequelize model file. Verify this is working by hitting http://localhost:3000/products and ensuring you see the product data returned as JSON.
Commit your changes and continue reading!
Now that we’ve got a working API and database we can deploy it to a server! In this article we’re using FL0, a platform that makes it really easy to deploy NodeJS apps to a containerized infrastructure, complete with database.
Up until now we’ve been committing changes to a local repo that only exists on our machine. Let’s link that to a Github repo so it can be stored in the cloud. In Github, create a new empty repo with no license or .gitignore file. If all goes well, you should see a screen like this:
We need to follow the steps under the heading “…or push an existing repository from the command line”. Note: I haven’t included the commands here because they will be specific to your own repo and may use HTTPS instead of SSH like mine does. It’s better to copy+paste from the Github page than this article for this step.
If successful, you should be able to refresh the Github page and see your code:
In FL0, create a new empty project and then add a Postgres database. You can call it anything you like.
Next, add a new Service and connect to your Github account:
You should see a window asking you to authorize FL0 to connect to your account:
When you are returned to FL0, your repos will be available to deploy through the user interface. Click the “Connect” button to continue. On the next page, you have the option to specify a Service Name and set Environment Variables. This is where we need to insert our Database URL.
Click the “+ Environment Variables” button and in the popup window that appears, select “Import database credentials”:
Make sure you see a row called !!DATABASE_URL!! before continuing.
Save your changes and click the “Deploy” button!
This will trigger a new build to begin. Click on your new Service and navigate to the Deployments tab to see how it’s progressing:
Once complete, go to the Overview tab and copy the URL:
Open it up in a browser and you should see the familiar “Hello World” message! Add a !!/products!! onto the end and you should see…nothing…yet! This is because our new FL0 database is still empty and we need to run the Seed script to insert the sample data.
Earlier we created a !!.env!! file that contained a !!DATABASE_URL!! variable instructing Sequelize to connect to our Docker container. Let’s edit the file to include a new variable, our FL0 database URL. You can find the Database URL in FL0 under the Connection Info tab of the Postgres resource.
Copy the !!.env!! and call it !!.env.dev!! and updated it as follows:
Adding this will let us run the Sequelize CLI against different environments. Try this:
Notice the !!dotenv-cli -e .env.dev!! option telling Sequelize to use FL0 instead of our local container. Go back to your browser and check the !!/products!! URL again. Hopefully you see some product JSON! Using FL0’s deployment features you can easily replicate your container to the Production environment with a completely separate database.
And there you have it folks, the building blocks for a successful NodeJS project! With the knowledge and skills you’ve gained from this article, you’re ready to bring your next big idea to life. We’d love to hear what you’re building in the comments below! We hope this article brought a smile to your face and a little inspiration to help tackle help your next project. Until next time, happy coding!
Tool and strategies modern teams need to help their companies grow.