Skip to content

Commit 3e9226d

Browse files
committed
chore: add OpenAPI docs and custom HTTP Port
1 parent a14ecde commit 3e9226d

File tree

4 files changed

+99
-5
lines changed

4 files changed

+99
-5
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,14 @@ After that you can start the server throught that:
1414

1515
```shell
1616
npm run start
17-
```
17+
```
18+
19+
The default port is `3000`. If you want to run it in other port you can define the `PORT` environment variable.
20+
21+
```shell
22+
PORT=3008 npm run start
23+
```
24+
25+
### Docs
26+
27+
You can access the path `/docs` to get access to Swagger Docs.

index.js

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,89 @@
11
import express from 'express';
22
import cors from 'cors';
33
import {getArticleMetaData} from 'article-metadata-extractor';
4+
import swagger from 'swagger-ui-express';
45

56
const app = express();
67

78
app.use(cors());
89

10+
const docs = {
11+
openapi: '3.0.0',
12+
paths: {
13+
"/": {
14+
"get": {
15+
"tags": ["Extractor"],
16+
"summary": "",
17+
"description": "",
18+
"parameters": [
19+
{
20+
"name": "url",
21+
"in": "query",
22+
"description": "Article's URL you want to extract some data",
23+
"required": true,
24+
"schema": {
25+
"type": "string",
26+
"format": "uri"
27+
}
28+
}
29+
],
30+
"responses": {
31+
"200": {
32+
"description": "Ok",
33+
"content": {
34+
"application/json": {
35+
"example": {
36+
"image": "https://www.designyourway.net/blog/wp-content/uploads/2015/03/XJbzrO.jpg",
37+
"title": "116 Cool CSS Text Effects Examples That You Can Download",
38+
"author": "Bogdan Sandu",
39+
"tags": [],
40+
"publicationDate": "2023-04-19T11:00:12.000Z",
41+
"readTime": 29,
42+
"description": "You came here for some really cool CSS text effects that will help you make amazing web typography for your websites.",
43+
"url": "https://www.designyourway.net/blog/yes-you-can-actually-make-these-text-effects-in-css/?utm_source=pocket_saves"
44+
}
45+
}
46+
}
47+
},
48+
"400": {
49+
"description": "Bad Request",
50+
"content": {
51+
"application/json": {
52+
"example": {
53+
"error": "Invalid URL"
54+
}
55+
}
56+
}
57+
},
58+
"500": {
59+
"description": "Internal Server Error",
60+
"content": {
61+
"application/json": {
62+
"example": {
63+
"error": "Ocorreu um erro interno"
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}
71+
},
72+
};
73+
74+
app.use('/docs', swagger.serve, swagger.setup(docs))
75+
976
app.get('/', (req, res) => {
1077
const {query} = req;
1178
if(!query?.url) {
12-
return res.status(400).send('Should defined query params url');
79+
const error = 'Should defined query params url';
80+
return res.status(400).send({error});
1381
}
1482
getArticleMetaData(query.url)
1583
.then((data) => res.json({...data, url: query.url}))
16-
.catch((erro) => res.status(500).send(erro));
84+
.catch((error) => res.status(500).send({error}));
1785
});
1886

19-
app.listen(3000, () => console.log('running'));
87+
const port = process.env.PORT || 3000
88+
89+
app.listen(port, () => console.log(`running at ${port}`));

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"dependencies": {
1414
"article-metadata-extractor": "^1.3.2",
1515
"cors": "^2.8.5",
16-
"express": "^4.18.2"
16+
"express": "^4.18.2",
17+
"swagger-ui-express": "^5.0.0"
1718
},
1819
"devDependencies": {
1920
"@types/express": "^4.17.18"

0 commit comments

Comments
 (0)