Result example as html: Movies Database API documentation
npm install -S express
npm install -S body-parser
npm install -S express2md
var express = require('express');
var bodyParser = require('body-parser');
var Markdown = require('express2md');
var app = express();
app.use(bodyParser.json());
var md = new Markdown({
express: app,
path: '/api.md', // path to API API documentation
storeResponses: true, // store first response as example
guessAll: true, // make description quite pretty
title: 'Movies Database',
baseUri: 'http://127.0.0.1:3000',
version: '1',
});
var movies = {
1: { name: 'Shaun of the Dead', year: 2004 },
2: { name: 'Hot Fuzz', year: 2007 },
};
app.get('/movies', function (req, res) { res.json(movies); });
app.post('/movies', function (req, res) {
if (movies[req.body.id]) res.status(409).json({ error: 'id exists' });
else res.status(201).json(movies[req.body.id] = req.body);
});
app.get('/movies/:id', function (req, res) {
if (movies[req.params.id]) res.json(movies[req.params.id]);
else res.status(404).json({ error: 'movie not found' });
});
app.delete('/movies/:id', function (req, res) {
if (movies[req.params.id]) res.json({ ok: delete movies[req.params.id] });
else res.status(404).json({ error: 'movie not found' });
});
app.get('/movies/:id/actors', function (req, res) { res.json({}); });
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
curl 127.0.0.1:3000/movies
{"1":{"name":"Shaun of the Dead","year":2004},"2":{"name":"Hot Fuzz","year":2007}}
curl 127.0.0.1:3000/movies/1
{"name":"Shaun of the Dead","year":2004}
curl 127.0.0.1:3000/movies/3
{"error":"movie not found"}
curl -X POST -H "Content-Type: application/json" -d '{"id":3,"name":"The World\u0027s End","year":2013}' 127.0.0.1:3000/movies
{"id":3,"name":"The World's End","year":2013}
curl 127.0.0.1:3000/movies
{"1":{"name":"Shaun of the Dead","year":2004},"2":{"name":"Hot Fuzz","year":2007},"3":{"id":3,"name":"The World's End","year":2013}}
curl -X POST -H "Content-Type: application/json" -d '{"id":3,"name":"Paul"}' 127.0.0.1:3000/movies
{"error":"id exists"}
curl -X POST -H "Content-Type: application/json" -d '{"id":4,"name":"Paul"}' 127.0.0.1:3000/movies
{"id":4,"name":"Paul"}
curl -X DELETE 127.0.0.1:3000/movies/4
{"ok":true}
curl -X DELETE 127.0.0.1:3000/movies/4
{"error":"movie not found"}
curl 127.0.0.1:3000/movies/1/actors
{}
curl 127.0.0.1:3000/api.md
# Movies Database v1 BaseUri: [http://127.0.0.1:3000](http://127.0.0.1:3000) # Methods ## Brief - [//api.md](#methods./api.md) - get - [//movies](#methods./movies) - [get](#methods./movies.get) - [post](#methods./movies.post) - [//movies/:id](#methods./movies/:id) - [get](#methods./movies/:id.get) - [delete](#methods./movies/:id.delete) - [//movies/:id/actors](#methods./movies/:id/actors) - [get](#methods./movies/:id/actors.get) ## /api.md Method | Description -------|------------ get | List all api.md ## /movies Method | Description -------|------------ get | List all movies post | Insert a new record in to movie collection ### get List all movies **Responses** code | type | example -----|------|-------- 200 |application/json | ```{"1":{"name":"Shaun of the Dead", "year":2004}, "2":{"name":"Hot Fuzz", "year":2007}}``` ### post Insert a new record in to movie collection **Body** type | body | example -----|------|-------- application/json | [](#types.) | {"id":3, "name":"The World's End", "year":2013} **Responses** code | type | example -----|------|-------- 201 |application/json | ```{"id":3, "name":"The World's End", "year":2013}``` 409 |application/json | ```{"error":"id exists"}``` ## /movies/{id} Method | Description -------|------------ get | Get movie with ID {id} delete | Delete movie with ID {id} ### get Get movie with ID {id} **Responses** code | type | example -----|------|-------- 200 |application/json | ```{"name":"Shaun of the Dead", "year":2004}``` 404 |application/json | ```{"error":"movie not found"}``` ### delete Delete movie with ID {id} **Responses** code | type | example -----|------|-------- 200 |application/json | ```{"ok":true}``` 404 |application/json | ```{"error":"movie not found"}``` ## /movies/{id}/actors Method | Description -------|------------ get | List of actors that movie ID {id} owns ### get List of actors that movie ID {id} owns **Responses** code | type | example -----|------|-------- 200 |application/json | ```{}```
Dimitry, 2@ivanoff.org.ua
curl -A cv ivanoff.org.ua