eolas/Programming_Languages/NodeJS/REST_APIs/0_Introduction.md
2022-08-04 08:30:04 +01:00

1 KiB

tags
Programming_Languages
backend
node-js
express
REST
apis

Creating a REST API with Node and Express: Introduction

We are going to use Express to create a RESTful API in Node.js.

Request types

Express provides us with methods corresponding to each of the HTTP request types:

  • app.get()
  • app.post()
  • app.put()
  • app.delete()

Creating an Express instance

const express = require('express')
const app = express()

Our data set

Typically when you create a RESTful API you are going to be returning data from a database. For simplicity we are just going simulate this with a simple data array so that we can focus on the Express syntax rather than database handling.

We will mainly work with the following array of objects:

const courses = [
  {
    id: 1,
    name: "First course",
  },
  {
    id: 2,
    name: "Second course",
  },
  {
    id: 3,
    name: "Third course",
  },
];