Skip to content

Latest commit

 

History

History

express

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Express.js - Verbose implementation

This is a kitchen sink style example covering all the ways you can adapt the Firetail middleware to match your project.

To get started

Using this implementation

cd javascript-quickstarts/express

Now we can install the samples dependencies

npm install

Now you can start the server

npm start

Try it out

Now that the server is started. Open a browser and go to http://localhost:3001

structure

express/
├── api/                ← 'api' is a hierarchy of files that map to operation IDs
│   ├── showPet/        ← Controllers under will to have the prefix "showPet."
│   │   └── byId.js     ← Controllers with the name "showPet.byId"
│   └── createPets.js   ← Controllers with the name "createPets"
└── animals.json        ← Sample data to be use
├── package.json        ← Project and Firetail configuration
├── petstore.yaml       ← The open API specification that this service will a here too
└── server.js           ← Entry point and wiring for the service

We can try some different types of requests

  • it's recommended when testing an API to use a tool such as postman

Endpoints exposed

  • GET: /pets ~ list all pets
  • POST: /pets ~ Create a pet
  • GET: /pets/{petId} ~ Retrieve a specific pet's details
  • DELETE: /pets/{petId} ~ Remove a specific pet

Each end-point is implemented slightly differently to give you a demonstration of the different ways in which you can integrate Firetail.


Use operations to map a controller to your opanAPI spec | GET: /pets

This endpoint list all pets. You can find the implementation at server.js ~ function listPets. It is load via the operations on server.js ~ firetailOpts


Use specificationDir to map a controller to your opanAPI spec | POST: /pets + Api token

fetch('http://localhost:3001/pets', {
  method: 'POST',
  headers: {
  'Content-Type': 'application/json',
  'X-Auth':'key'
  },
  body: JSON.stringify({ "name": "Mr Beast", "tag": "dog" })
})
.then((response) => response.json())
.then((data) => console.log('Success:', data))
.catch((error) => console.error('Error:', error));

This endpoint is to Create a pet. You can find the implementation at api/createPets.js. It is load via the specificationDir in package.json. This Endpoint is configured to require an API token. The security is connected in petstore.yaml with the token validation being implemented at server.js ~ authCallbacks


Use nasted specificationDir to map a controller to your opanAPI spec | GET: /pets/{petId} + oauth2

fetch('http://localhost:3001/pets/2', {
  method: 'GET',
  headers: {
  'authorization':'Bearer RsT5OjbzRn430zqMLgV3Ia'
  }
})
.then((response) => response.json())
.then((data) => console.log('Success:', data))
.catch((error) => console.error('Error:', error));

This endpoint retrieve specific pet based on the ID pass in the URL. You can find the implementation at api/showPet/byId.js. It is load via the specificationDir in package.json. This Endpoint is configured to require an oAuth2 bearer token. The security is connected in petstore.yaml with the validation implemented at server.js ~ authCallbacks


Use Express's native routing mechanism | DELETE: /pets/{petId} + JWT

fetch('http://localhost:3001/pets/2', {
  method: 'DELETE',
  headers: {
  'authorization':"bearer eyJ1c2VyIjoxMjN9"
  }
})
.then((response) => response.json())
.then((data) => console.log('Success:', data))
.catch((error) => console.error('Error:', error));

This endpoint retrieve specific pet based on the ID pass in the URL. You can find the implementation at api/showPet/byId.js. It is load via the specificationDir in package.json. This Endpoint is configured to require an oAuth2 bearer token. The security is connected in petstore.yaml with the validation implemented at server.js ~ authCallbacks