This project is a simplified feel of Swagger, which helps you test APIs in your ExpressJS project with a GUI interface.
-
Download zip and extract locally. Open with your text editor software.
-
In the terminal, navigate to the root directory of API-Documentation where you see index.html. Run
npm ito install dependencies. -
Open api.json file (example template) to provide necessary information.
info.title: Your project nameinfo.description: Your project descriptionhost: Your ExpressJS project's domain addressbasePath: Your project base routeschemes: Protocols your project uses (💡 array)paths: (💡 array of objects)*endpoint*: each endpoint that comes afterbasePath(👉🏼 please use curly braces, {}, for path parameter. e.g, /friends/{id})*HTTP method*: get, post, put, or deletepaths.*endpoint*.*HTTP method*.tags: the purpose of this endpointpaths.*endpoint*.*HTTP method*.summary: the summary of this endpointpaths.*endpoint*.*HTTP method*.parameters: (💡 array of objects. 👉🏼if there is no parameter, skip.)name: parameter namein: parameter type (select from 3 types: query, path, body)required: Booleandescription: the description of your parameter
paths.*endpoint*.*HTTP method*.responses: "HTTP status code": {"description":"description of the HTTP status code"}
-
If your ExpressJS project doesn't have cors installed,
npm install cors
[Example of CORS configuration 1 - Allowing specific origin]
var express = require('express')
var cors = require('cors')
var app = express()
var corsOptions = {
origin: '{API-Documentation-App's domain address, e.g., http://localhost:3001, the port number changes depending on which ports are in use}',
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
app.get('/products/:id', cors(corsOptions), function (req, res, next) {
res.json({msg: 'This is CORS-enabled for only example.com.'})
})
app.listen(80, function () {
console.log('CORS-enabled web server listening on port 80')
})
[Example of CORS configuration 2 - Enabling all CORS requests]
var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors())
app.get('/products/:id', function (req, res, next) {
res.json({msg: 'This is CORS-enabled for all origins!'})
})
app.listen(80, function () {
console.log('CORS-enabled web server listening on port 80')
})
- Start your ExpressJS project.
- In the terminal, go to API-Documentation's root directory. run this command
npm start, which should display all the configurations in api.json. Check domain address and confirm it's the same inoriginvalue in corsOptions at Step3.
- HTTP methods avaialble are GET, POST, PUT, DELETE.