1
1
import express from 'express' ;
2
2
import cors from 'cors' ;
3
3
import { getArticleMetaData } from 'article-metadata-extractor' ;
4
+ import swagger from 'swagger-ui-express' ;
4
5
5
6
const app = express ( ) ;
6
7
7
8
app . use ( cors ( ) ) ;
8
9
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
+
9
76
app . get ( '/' , ( req , res ) => {
10
77
const { query} = req ;
11
78
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} ) ;
13
81
}
14
82
getArticleMetaData ( query . url )
15
83
. then ( ( data ) => res . json ( { ...data , url : query . url } ) )
16
- . catch ( ( erro ) => res . status ( 500 ) . send ( erro ) ) ;
84
+ . catch ( ( error ) => res . status ( 500 ) . send ( { error } ) ) ;
17
85
} ) ;
18
86
19
- app . listen ( 3000 , ( ) => console . log ( 'running' ) ) ;
87
+ const port = process . env . PORT || 3000
88
+
89
+ app . listen ( port , ( ) => console . log ( `running at ${ port } ` ) ) ;
0 commit comments