11import express from "express" ;
22import axios from "axios" ;
3+ import { parseXML } from "./helper.js" ;
34
45const app = express ( ) ;
56app . disable ( "x-powered-by" ) ;
@@ -23,6 +24,8 @@ app.get("/rss", async (req, res) => {
2324 const sites = {
2425 "frontend-focus" : "https://cprss.s3.amazonaws.com/frontendfoc.us.xml" ,
2526 "react-status" : "https://cprss.s3.amazonaws.com/react.statuscode.com.xml" ,
27+ "news-api" :
28+ "https://raw.githubusercontent.com/lifeparticle/binarytree/main/api/news/news.json" ,
2629 } ;
2730 const response = await axios . get ( sites [ sitename ] , {
2831 responseType : "arraybuffer" ,
@@ -33,9 +36,21 @@ app.get("/rss", async (req, res) => {
3336 }
3437
3538 res . setHeader ( "Cache-Control" , "s-max-age=86400, stale-while-revalidate" ) ;
36- res . set ( "Content-Type" , "application/xml " ) ;
39+ res . set ( "Content-Type" , "application/json " ) ;
3740
38- res . send ( response . data ) ;
41+ if ( sitename === "news-api" ) res . send ( response . data ) ;
42+
43+ const xmlData = response . data . toString ( ) ;
44+
45+ parseXML ( xmlData )
46+ . then ( ( parsedData ) => {
47+ console . log ( parsedData ) ;
48+ res . send ( { articles : parsedData } ) ;
49+ } )
50+ . catch ( ( error ) => {
51+ console . error ( "Error parsing XML:" , error ) ;
52+ res . status ( 500 ) . json ( { error : "Error parsing XML" } ) ;
53+ } ) ;
3954 } catch ( error ) {
4055 if ( error instanceof Error ) {
4156 res . status ( 500 ) . json ( { type : "error" , message : error . message } ) ;
0 commit comments