/be-middlewares-practice

An express server which currently contains no middleware

Primary LanguageJavaScript

Express Middleware

This repo contains a demo to go with the Express Middleware Backend Lecture.It is a small API that contains the following routes:

GET / - Should Return the string "hello World!", along with a 200 status code

GET /jokes - Should return an array of all jokes

GET /randomJoke - Should return a random joke from the available ones

POST /jokes - Accepts the following body:

{
    "name": String,
    "content": String
}

Should add a new joke to the database and return a 201 status code. This endpoint may need some middleware adding to allow the reading of the request body...

Middleware challenges

The following functionality has not been built into the application. You will need to use middleware to acheive the following:

  • ALL requests should be logged when received. Morgan may be useful for this.

  • CORS should be enabled only on the / endpoint. This would mean that only this endpoint would not be accessible in the browser.

  • If a client visits /hello.html, they should be served a html file displaying the text "This is an Express Server!"

  • Any /jokes routes require the request to contain an Authorization header containing the string "supersecret". If not present, or if the string is not correct, the server should return a 401 status code.

  • ALL endpoints should use helmet to improve security on the endpoints.

Setup

Install dependencies

To install required deps, run the following:

npm i

Database

To set the database up, run the following command to build the docker image defined in the repo:

docker build . -t commanddshift/express-middleware

To build the image, run:

docker run -d -p 5435:5432 --name express-middleware -e POSTGRES_PASSWORD=password -d commanddshift/express-middleware

Environment variables

Create a .env file in the root of the repo with the following values:

PGUSER=postgres
PGHOST=localhost
PGPASSWORD=password
PGDATABASE=middlewares
PGPORT=5435
PORT=3000

This contains credentials to allow a connection to the database.