Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting error #33

Open
itechdivyanshu opened this issue Aug 7, 2020 · 3 comments
Open

Getting error #33

itechdivyanshu opened this issue Aug 7, 2020 · 3 comments

Comments

@itechdivyanshu
Copy link

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).

@4ashk1n
Copy link

4ashk1n commented Jul 30, 2022

Hello, how did you fix it?

@omerturkerweb
Copy link

any solve?

@kelvinpraises
Copy link

Basically need cors to work
https://www.npmjs.com/package/cors
#13 (comment)

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')
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants