-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
91 lines (79 loc) · 2.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const express = require('express');
const swaggerDocs = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const app = express();
// Set up Swagger Options
const options = {
failOnErrors: true, // Whether or not to throw when parsing errors. Defaults to false.
definition: {
openapi: '3.0.0',
info: {
title: 'CommandShift Cohorts Greetings',
version: '18.12.2023',
description: "Hello greetings for different bootcamp cohorts"
},
},
// Remember to change this to match the files where the endpoint definitions should be found.
// They can be hardcoded or you can use wildcard to match on multiple paths e.g. ./src/*.js
apis: ['./index.js'],
};
// Link up API Specfications with Swagger and the path to serve the documentation
const openapiSpecification = swaggerDocs(options);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(openapiSpecification));
app.use(express.json());
app.get("/", (_, response) => {
response.send();
});
// Stateful examples - where the logic
app.get("/v1/hellosept23", (request, response) => {
response.status(200).send("Hello, Sept2023!");
})
app.get("/v1/hellojan24", (request, response) => {
response.status(200).send("Hello, Jan2024!");
})
// Stateless examples - where the logic does not make assumption / has hardcoded values
// Documented API using OpenApi and presented by Swagger
// Versioned Routes following best practices (lowercase naming)
/**
* @openapi
* /v1/cohorts/{cohort}/hello:
* get:
* description: Returns a string with a cohort
* parameters:
* - in: path
* name: cohort
* schema:
* type: string
* example: Sept 2023
* responses:
* 200:
* description: Returns a successful response with a short greeting for passed cohort name.
* 400:
* description: The request was wrong.
*/
app.get("/v1/cohorts/:cohort/hello", (request, response) => {
const cohortValue = request.params.cohort;
response.status(200).send(`Hello, ${cohortValue}!`);
})
/**
* @openapi
* /v2/cohorts/{cohort}/hello:
* get:
* description: Returns a long hello string with a cohort
* parameters:
* - in: path
* name: cohort
* schema:
* type: string
* example: Sept 2023
* responses:
* 200:
* description: Returns a successful response with a long greetings for passed cohort name.
* 400:
* description: The request was wrong.
*/
app.get("/v2/cohorts/:cohort/hello", (request, response) => {
const cohortValue = request.params.cohort;
response.status(200).send(`Greetings fellow one, you belong to the ${cohortValue} cohort, are you not?!`);
})
app.listen(3000, () => { console.log("Server is running on port 3000. Enjoy the Greetings API!") });