Giter VIP home page Giter VIP logo

express-cors-anywhere's Introduction

express-cors-anywhere

An express middleware for redirecting requests and avoiding CORS errors when hitting cross-origin APIs.

๐Ÿš€ Getting Started

Using yarn:

yarn add express-cors-anywhere

โœ๏ธ Usage

Just embed the default export at the route you wish to redirect requests from:

import express from "express";
import axios from "axios";
import { json } from "body-parser";
import anywhere from "express-cors-anywhere";

const { PORT } = process.env;
const port = PORT || 3000;

(async () => {
  /* configure cors */
  await new Promise(
    resolve => express()
      .use(json())
      .use("/cors-anywhere", anywhere())
      .get("/hello", (_, res) => res.status(200).send("Hi!"))
      .listen(port, resolve),
  );
})();

Subsequently, all requests made to http://localhost:3000/cors-anywhere will be redirected from your server without cors headers using an identical format to CORS Anywhere. All you need to do is prefix the location of your middleware route to the required target URL.

As an example, say you wish to POST to a cross-origin API at https://some-example-api.com/some-example-endpoint from your browser. This would not work from your browser because a) it does not exist, and b) it relies on a resource provided by another origin. However, we can just make the same request to the middleware address instead:

import axios from "axios";

(async () => {
  const { data, status } = await axios({
    url: "http://localhost:3000/cors-anywhere/https://some-example-api/some-example-endpoint",
    method: "post",
    headers: {
      "Content-Type": "application/json",
    },
    data: {
      some: "data",
    },
  });
  /* data returned is from https://some-example-api/some-example-endpoint */
  console.log({ data });
});

โœŒ๏ธ License

MIT

express-cors-anywhere's People

Contributors

cawfree avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mdiabi

express-cors-anywhere's Issues

Error: connect ECONNREFUSED 127.0.0.1:80

I am trying to use your middleware. When I start my server and go to localhost:3000/cors-anywhere, I would expect to see the cors-anywhere text, but I get this:

Error: connect ECONNREFUSED 127.0.0.1:80
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1056:14)

My code is simply:

import express from 'express';
import cors from 'cors';
import anywhere from 'express-cors-anywhere';
import path from 'path';

const { PORT } = process.env;
const port = PORT || 3030;

var app = express();

app.use(cors());
app.use('/cors-anywhere', anywhere());

app.get('*', (req, res) => {
	console.log('gotten');
	res.sendFile(path.resolve(__dirname, 'index.html'));
});

app.listen(port, () => {
	console.log(`App listening on port ${port}`);
});

I get the same error when I use the example code from your readme:

import express from "express";
import axios from "axios";
import { json } from "body-parser";
import anywhere from "express-cors-anywhere";

const { PORT } = process.env;
const port = PORT || 3000;

(async () => {
  /* configure cors */
  await new Promise(
    resolve => express()
      .use(json())
      .use("/cors-anywhere", anywhere())
      .get("/hello", (_, res) => res.status(200).send("Hi!"))
      .listen(port, resolve),
  );
})();

Is this error expected?

error anywhere not function

C:\Users\LENOVO\Pictures\mainan\corsany\api\index.js:14
.use('/cors-anywhere', anywhere())
^

TypeError: anywhere is not a function
at C:\Users\LENOVO\Pictures\mainan\corsany\api\index.js:14:30
at new Promise ()
at C:\Users\LENOVO\Pictures\mainan\corsany\api\index.js:11:9
at Object. (C:\Users\LENOVO\Pictures\mainan\corsany\api\index.js:18:3)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
at node:internal/main/run_main_module:28:49

code
const express = require('express');
const axios = require('axios');
const { json } = require('body-parser');
const anywhere = require('express-cors-anywhere');

const { PORT } = process.env;
const port = PORT || 3000;

(async () => {
/* configure cors */
await new Promise(resolve =>
express()
.use(json())
.use('/cors-anywhere', anywhere())
.get('/hello', (_, res) => res.status(200).send('Hi!'))
.listen(port, resolve)
);
})();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.