Skip to content

Parts of the spec file

Course: OpenAPI Spec - chapter 3

For a simple server

server.js

import express from 'express';
const app = express();
const port = 3000;
app.use(express.json());
let users = [
{ id: 1, name: 'John Doe' },
{ id: 2, name: 'Jane Doe' }
];
app.get('/users', (req, res) => {
const { name } = req.query;
if (name) {
const filteredUsers = users.filter(user => user.name.toLowerCase().includes(name.toLowerCase()));
res.json(filteredUsers);
} else {
res.json(users);
}
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});

OpenAPI Spec

openapi: 3.0.0
info:
title: User API
description: API to manage users
version: "1.0.0"
servers:
- url: http://localhost:3000
paths:
/users:
get:
summary: Get a list of users
description: Retrieves a list of users, optionally filtered by name.
parameters:
- in: query
name: name
schema:
type: string
required: false
description: Name filter for user lookup.
responses:
'200':
description: A list of users
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/User'
components:
schemas:
User:
type: object
properties:
id:
type: integer
format: int64
description: The unique identifier of the user.
name:
type: string
description: The name of the user.
required:
- id
- name

Try visiting

http://localhost:3000/users?name=John Doe