link icon indicating copy to clipboard operation
link copied to clipboard

Getting error

Open itechdivyanshu opened this issue 5 years ago • 3 comments

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://pravosleva.ru/express-helper/url-metadata/editorjs?url=https%3A%2F%2Fgoogle.com. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

itechdivyanshu avatar Aug 07 '20 07:08 itechdivyanshu

Hello, how did you fix it?

4ashk1n avatar Jul 30 '22 05:07 4ashk1n

any solve?

omerturkerweb avatar Apr 14 '23 13:04 omerturkerweb

Basically need cors to work https://www.npmjs.com/package/cors https://github.com/editor-js/link/issues/13#issuecomment-739124948

import * as cheerio from "cheerio";
import express from "express";
var cors = require("cors");

const app = express();

app.use(cors());

// cheerio is used for faster implementation
// https://www.npmjs.com/package/cheerio
// editorjs link previewer
app.get("/fetchUrl", async (req, res) => {
  let url = req.query.url as string;
  let resHTML = await fetch(new URL(url)).catch((e) => console.log(e));

  if (!resHTML) {
    res.status(500).json({
      success: 0,
      meta: {},
    });
    return;
  }

  const html = await resHTML.text();
  const $ = cheerio.load(html);

  // custom meta-tag function
  const getMetaTag = (value: string) => {
    return (
      $(`meta[name=${value}]`).attr("content") ||
      $(`meta[property="og:${value}"]`).attr("content") ||
      $(`meta[property="twitter:${value}"]`).attr("content")
    );
  };

  const metadataObject = {
    success: 1,
    meta: {
      title: $("title").first().text(),
      description: getMetaTag("description"),
      image: {
        url: getMetaTag("image"),
      },
    },
  };
  res.send(metadataObject);
  return;
});

app.listen(80, function () {
  console.log('All CORS Requests-enabled web server listening on port 80')
})

kelvinpraises avatar Sep 25 '23 22:09 kelvinpraises