JAMstack NodeJS API

JAMstack

LAMP and MEAN are stacks of past.

For those unfamiliar, a JAMstack is built on 3 ideaologies: JavaScript, API, Markup

In this post, we’ll be creating the API portion that stores flavors of jam [strawberry, grape, etc.] and an MSRP value.

HTTP

I’ve had professional experience working with HTTP, so all of the methods were pretty familiar to me.

If you’re new to this space, here’s a quick rundown of the HTTP methods that we will be using:

  • POST - send data
  • GET - retrieve data
  • PUT - update data
  • DELETE - remove data

Seem familar? CRUD. :)

NodeJS

Learning to write an API in my development journey has been full of Google Searches, StackOverflow posts, and Quora questions, but piecing together all of the tutorials and bridging the subsequent knowledge gaps was pretty difficult.

Here is an article by Scott Domes that helped me out the most. There’s also a video if that’s your cup of tea.

His tutorial creates routes for notes, but I’ve modified it to store types of jam.

Source Code

If you run through Scott’s tutorial, but prefer to use the local database we made in the last post, be sure that your config/db.js file looks like this:

# config/db.js
module.exports = {
    url: "mongodb://localhost:27017"
};

The only addition to the tutorial I would suggest is a default GET route. Without specifying an ID in the URI, an API should return all elements of that resource type.

app.get('/jam', (req, res) => {
    db.collection('jam', (err, collection) => {
        collection.find().toArray((err,items) => {
            res.send(items)
        });
    });
})

Next Up

Dockerizing!

We’ll stand up docker containers for MongoDB + this API, then use Docker Compose to create a deployable stack.