@@ -6,6 +6,7 @@ const { pronounce } = require("node-pronounce");
6
6
const randomUseragent = require ( "random-useragent" ) ;
7
7
const rua = randomUseragent . getRandom ( ) ;
8
8
var wordOfDay = [ ] ;
9
+ const baseUrl = 'https://randomword.com' ;
9
10
10
11
router . get ( "/" , function ( req , res ) {
11
12
res . header ( "Access-Control-Allow-Origin" , "*" ) ;
@@ -22,7 +23,73 @@ router.get("/", function (req, res) {
22
23
23
24
axios ( {
24
25
method : "GET" ,
25
- url : "https://randomword.com/" ,
26
+ url : baseUrl ,
27
+ headers : {
28
+ "User-Agent" : rua ,
29
+ } ,
30
+ } )
31
+ . then ( function ( response ) {
32
+ $ = cheerio . load ( response . data ) ;
33
+ if ( wordOfDay . length > 0 ) {
34
+ wordOfDay = [ ] ;
35
+ }
36
+
37
+ var post = $ ( ".section #shared_section" ) ;
38
+ var word = post
39
+ . find ( "#random_word" )
40
+ . eq ( 0 )
41
+ . text ( )
42
+ . replace ( "\r\n\t\t\t\t\t" , "" )
43
+ . replace ( "\r\n\t\t\t\t" , "" )
44
+ . replace ( "\n\t\t\t\t\t" , "" )
45
+ . replace ( "\n\t\t\t\t" , "" ) ;
46
+ var definition = post
47
+ . find ( "#random_word_definition" )
48
+ . eq ( 0 )
49
+ . text ( )
50
+ . replace ( "\n" , "" ) ;
51
+ var pronounceword = pronounce ( word ) . replace ( "," , "" ) ;
52
+
53
+ wordOfDay . push ( {
54
+ word : decodeURI ( word . charAt ( 0 ) . toUpperCase ( ) + word . slice ( 1 ) ) ,
55
+ definition : decodeURI (
56
+ definition . charAt ( 0 ) . toUpperCase ( ) + definition . slice ( 1 )
57
+ ) ,
58
+ pronunciation : decodeURI (
59
+ pronounceword . charAt ( 0 ) . toUpperCase ( ) + pronounceword . slice ( 1 )
60
+ ) ,
61
+ } ) ;
62
+ res . send ( JSON . stringify ( wordOfDay , null , 2 ) ) ;
63
+ } )
64
+ . catch ( function ( error ) {
65
+ if ( ! error . response ) {
66
+ console . log ( "API URL is Missing" ) ;
67
+ res . json ( "API URL is Missing" ) ;
68
+ } else {
69
+ console . log ( "Something Went Wrong - Enter the Correct API URL" ) ;
70
+ res . json ( "Something Went Wrong - Enter the Correct API URL" ) ;
71
+ }
72
+ } ) ;
73
+ } ) ;
74
+
75
+ router . get ( "/:pos" , function ( req , res ) {
76
+ res . header ( "Access-Control-Allow-Origin" , "*" ) ;
77
+ res . header (
78
+ "Access-Control-Allow-Headers" ,
79
+ "Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With"
80
+ ) ;
81
+ res . header ( "Access-Control-Allow-Methods" , "GET" ) ;
82
+ res . header ( "X-Frame-Options" , "DENY" ) ;
83
+ res . header ( "X-XSS-Protection" , "1; mode=block" ) ;
84
+ res . header ( "X-Content-Type-Options" , "nosniff" ) ;
85
+ res . header ( "Strict-Transport-Security" , "max-age=63072000" ) ;
86
+ res . setHeader ( "Content-Type" , "application/json" ) ;
87
+
88
+ const partOfSpeech = req . params . pos ;
89
+
90
+ axios ( {
91
+ method : "GET" ,
92
+ url : `${ baseUrl } /${ partOfSpeech } ` ,
26
93
headers : {
27
94
"User-Agent" : rua ,
28
95
} ,
0 commit comments