GithubHelp home page GithubHelp logo

api-express-counter's Introduction

Express Counter

In this exercise we will build a simple counter API using express.

Learning Objectives

  • Explain how to set up and run an Express application
  • Build a simple Express API and send a HTTP request to an available endpoint using a HTTP client
  • Explain that JSON is a widely-used language-agnostic format for encoding data as attribute value pairs
  • Diagram the request and response cycle for an Express API.
  • Use console.log() and read the result in the server logs

Express Setup

To set up a new express application, in an empty folder follow these steps.

  1. Initialize npm. You can accept all the defaults.
npm init
  1. Install the express library
npm install express
  1. Install nodemon. nodemon is a utility tool that will automatically restart our express server any time we change code.
npm install --save-dev nodemon
  1. Install the morgan middleware. Express middleware allows us to extend express with additional functionality. Morgan is express middleware we can use to have our server automatically log all requests. This helps gives us visibility of what our server is doing.
npm install morgan
  1. Install the cors middleware. This allows us to make HTTP requests to our API using fetch from the browser.
npm install cors
  1. Inside a /src directory, create two files.
  • Create a file that will run our app: index.js . This is our entrypoint - the source file that will start running our server.
  • Create a file that will define what our app does: server.js
  1. In the app runner, add this code:
const app = require('./server.js')
const port = 3030

app.listen(port, () => {
 console.log(`Server is running on http://localhost:${port}/`)
})
  1. In the file that defines what the app does:
//Include the express library
const express = require("express")
//Include the morgan middleware
const morgan = require("morgan")
//Include the cors middleware
const cors = require("cors")

//Create a new express application
const app = express()

//Tell express we want to use the morgan library
app.use(morgan("dev"))
//Tell express we want to use the cors library
app.use(cors())
//Tell express to parse JSON in the request body
app.use(express.json())

module.exports = app
  1. Update your package.json file and replace the "scripts" section with the following:
"scripts": {
   "start" : "npx nodemon ./src/index.js"
},
  1. Finally, start up our server!
npm start

Our servers is now running and listening for HTTP requests.

Instructions

Your application should maintain a simple numeric counter and provide the endpoints detailed in the API Spec.

Each route should be detailed in a sequence diagram in the same way you did during the API address book exercise.

Test each route using:

  • fetch
  • curl
  • An API testing tool such as postman or insomnia

Running tests

Run the following commands from your project directory to run the test suites:

$ npm test # standard criteria
$ npm run test-extensions # extension criteria

You can also focus on one test at a time - use the jest docs to help filter which tests to run. We recommend you run tests manually with the option --forceExit.

For example, for the following test:

it("describes an invidual test", async () => {
  const response = await supertest(app).get("/books")

  expect(response.status).toEqual(200)
})

Here are two ways to run it.

$ npx jest -t "describes an invidual test" --forceExit
$ npx jest test/api/routes/books.spec.js --forceExit # remember to add the 'f' before it()

Extension 1

Add a route as detailed in the API Spec Extension 1. For example, making a PUT request to /counter?value=20 should set the value of the counter to 20. Use the req.query property in your callback to get the value provided. See the express documentation. If no value is provided, the counter should not be changed.

Extension 2

Using route parameters, update your API to keep track of multiple counters. Allow the client to specify the counter name as part of the URL. With this in mind, build the endpoints as detailed in the API Spec Extension 2.

api-express-counter's People

Contributors

dearshrewdwit avatar dependabot[bot] avatar mikemherron avatar rockettdev avatar vherus avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.