I've two files on js file for returning JSON response if url has /links in it. Another gets called if there's default url path. I'am using router for this purpose.it's not working. It seemed to work yesterday

//This is for the default URL path
var links = [

{"name": "LinkedIn", "url": "https://www.linkedin.com/in/madhura-suryawanshi/" },
{"name": "Instagram", "url": "https://linkurl" },
{"name": "Github", "url": "https://linkurl" },

]
const url = “https://static-links-page.signalnerve.workers.dev

async function handleRequest() {
const init = {
headers: {
“content-type”: “text/html;charset=UTF-8”,
},
}
const res = await fetch(url,init)
// const response = await fetch(url, init)

return rewriter.transform(res)

}
class ProfileBuilder {

      element(element) {
        element.removeAttribute("style")

      }

}

class LinksTransformer{
constructor() {
this.links = links
}

        element(element) {
            
            // for (const [text, link] in Object.entries(this.links)) {
                
                element.append(`<a href="${links}">asdf</a>`, { html: true })
            // }
        }     
  
}

class ImageAdder{
constructor(attributeName) {
this.attributeName = attributeName
}
element(element) {

      element.setAttribute(
        this.attributeName,
        "tuffy.jpeg"
      )
    
  }

}
class NameSetter{
constructor(attributeName) {
this.attributeName = attributeName
}
element(element) {

      element.prepend(
        "Madhura Suryawanshi",
      )
    
  }

}

const rewriter = new HTMLRewriter()
.on(“div#links”, new LinksTransformer())
.on(“div#profile”, new ProfileBuilder())
.on(“img#avatar”, new ImageAdder(“src”))
.on(“h1#name”, new NameSetter(“text”))

addEventListener(“fetch”, event => {
return event.respondWith(handleRequest())
})
module.export = links

//This is for URL with /links
import Router from ‘./router’
import profile from ‘./handlers/profile’
import links from ‘./handlers/profile’

addEventListener(‘fetch’, event => {
event.respondWith(handleRequest(event.request))
})

function handler(request) {

const init = {
    headers: { 'content-type': 'application/json' },
}
const body = JSON.stringify(links,null,2)
return new Response(body, init)

}

async function handleRequest(request) {
const r = new Router()

r.get('/links', request => handler(request))
r.get('/', profile)

// return r.route(request)

const resp = await r.route(request)
return resp

}

This topic was automatically closed after 31 days. New replies are no longer allowed.