/web-107-node-mongoose-tutorial

Tutorial on using the mongoose ORM for NodeJS and MongoDB

Primary LanguageTypeScript

Mongoose Tutorial

Simple REST endpoint using express and the Mongoose ORM.

The tutorial uses Express Routes and Controllers to manage the complexity of a REST API. Controllers define the business logic, models define the document structure in the database, and routes are how we expose our business logic (controllers) as an API.

You will find a basic CRUD (Create, Read, Update, Delete) route for a blog in the blog.ts file.

Install

This project uses the yarn package manager.

# Install yarn
npm install -g yarn

# install dependencies with the yarn package manager
yarn install

Build

# start mongodb docker container
yarn start-db 

# compile typescript
yarn build

# add demo data
yarn seed

# start express server
yarn start

Examples

See GET, PUT, DELETE, and POST examples in the requests.http file.

Below you can test the endpoint with the curl command.

# GET blog post by ID

curl -X GET --location "http://localhost:8080/api/blog/29a95f357e10e2b0ffdd4ccb" \
    -H "Accept: application/json"


# GET all blogs posts
curl -X GET --location "http://localhost:8080/api/blogs" \
    -H "Accept: application/json"


# PUT update blog post by ID
curl -X PUT --location "http://localhost:8080/api/blog/29a95f357e10e2b0ffdd4ccb" \
    -H "Accept: application/json" \
    -H "Content-Type: application/json" \
    -d "{
          \"title\": \"updated title\"
        }"


# DELETE blog post by ID
curl -X DELETE --location "http://localhost:8080/api/blog/29a95f357e10e2b0ffdd4ccb" \
    -H "Accept: application/json"


# POST (Create blog post)
curl -X POST --location "http://localhost:8080/api/blog" \
    -H "Accept: application/json" \
    -H "Content-Type: application/json" \
    -d "{
          \"title\": \"My Blog Title\",
          \"content\": \"This is my blog\",
          \"author\": \"Nik Jmaeff\"
        }"