Building A Basic Node Backend

Adam Chernitsky
5 min readMar 2, 2020

--

For this blog I will be covering how to make a basic node backend with Knex and Express. Node is a javascript backend framework, and one benefit of this is if your frontend is in JavaScript you’ll be doing nothing but JavaScript the whole way through your project. To start off with your Node backend make a new directory that you plan to build your backend in. Once you have created this directory we are going to install a few things, run a few commands, and create some folders that we will be working in:

This creates your package.json file and your knexfile.js the rest will install dependencies you’ll need for the app:

After installing these create an app.js file:

Next in your app.js file your going to set up the start of your Express app:

Now that you’ve done this we are going to add a couple more files and folders that we will be working in:

Inside this dogs.js file we will have all of the routes pertaining to dogs, these routes will deal with the database table for dogs. In this file you’ll need to add the following code:

Now in app.js we need to require this router:

Now we need to mount this router on the app:

Now all of the routes that are defined in dogs.js file are available to the app. Next we’re going to create a start script in our package.json file and in this file we’re going to edit the main. We’ll also add a dev script. Here is the original:

Original

Here is what it should look like after you edit it:

Now you can start your server by running this command:

You know that you’ve successfully set this up if you see Listening on 300:

Now that your server is running use Postman to verify that your get route we created in routes/dogs.js is working properly. In Postman type in the url, which will be http://localhost:3000/dogs and make sure that you are doing a get request. If done correctly an empty array should be returned, Postman should look like so:

Next we want to be able to query out database from our get route so we can get the data from the dogs table. So we’re going to create a database connection that we can reuse throughout our app. To do this we’re going to create a db folder and inside this we are going to have a database.js file:

Here is what we are going to put in this file.

Now if you want to connect to the database anywhere in your app you just have to require in this file. Next we want to create a place where all of our database queries will go. To do this we will create a new file in the db folder:

This file is going to export any queries you may use in your app. The code in this app should look like this:

The getAll function here is what is going to query the database. Next we are going to need to go back to routes/dogs.js file and require in the queries file that we just built:

After we require in queries we make a get request to /dogs and we then query the database for all dogs and we get it back as an array and send it back as a JSON response. Next we are going to create our migration file:

This Knex migrate command will create you migrations folder as well as your first migrations file and database table. In this file you’ll need to add your desired attributes/columns:

After creating your migration and adding your columns run the command:

Next we’ll create our seeds file and folder and add a seed:

Once you create your seed you need to run your seed file:

Finally go to your Postman to verify that everything is working:

If done correctly you should see your seed show up in Postman.

--

--