Sign in New API Help About

803 bytes of JavaScript
Created 5 hours ago — expires in 7 days
Viewed 22 times
https://dpaste.com/8EBVPCT9Q
COPY TO CLIPBOARD SOFT WRAP RAW TEXT DUPLICATE DIFF
const express = require('express');
const app = express();
const cors = require('cors');


const allowedOrigins = ['http://localhost:3000', 'http://example.com'];

app.use(cors({
  origin: function(origin, callback) {
    // Allow requests with no origin (like mobile apps or curl requests)
    if (!origin) return callback(null, true);

    if (allowedOrigins.indexOf(origin) === -1) {
      const msg = 'The CORS policy for this site does not allow access from the specified Origin.';
      return callback(new Error(msg), false);
    }

    return callback(null, true);
  }
}));
const port = 3000;

app.get("/api/data", (req, res, next) => {
 res.json(["Tony","Lisa","Michael","Ginger","Food"]);
});

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

Share: