How to use corser - 10 common examples

To help you get started, we’ve selected a few corser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kenju / instagram_js_filter / src / server / server.js View on Github external
return new Promise(resolve => {
        app.use(bodyParser.urlencoded({
            extended: true
        }));
        app.use(bodyParser.json());
        // TODO:
        app.use(morgan('dev'));
        app.use(helmet());
        app.use(corser.create());
        resolve(app);
    });
};
github origami-cms / cms / packages / core-server / src / Server.ts View on Github external
private async _setup() {
    // Setup the store
    if (this.store) {
      this.app.set('store', this.store);
    }

    this.app.use(fileUpload());
    this.app.use(
      helmet({
        frameguard: {
          action: 'allow-from',
          domain: '*'
        }
      })
    );
    this.app.use(corser.create());

    await this._setupStatic();

    // Setup the middleware
    await this._setupMiddleware();
  }
github derhuerst / db-rest / api.js View on Github external
const journeys = require('./lib/journeys')
const stations = require('./lib/stations')
const allStations = require('./lib/all-stations')



const api = express()
module.exports = api

api.use(hsts({maxAge: 24 * 60 * 60 * 1000}))

morgan.token('id', (req, res) => req.headers['x-identifier'] || shorthash(req.ip))
api.use(morgan(':date[iso] :id :method :url :status :response-time ms'))

const allowed = corser.simpleRequestHeaders.concat(['User-Agent', 'X-Identifier'])
api.use(corser.create({requestHeaders: allowed})) // CORS

api.use(compression())

api.use((req, res, next) => {
	if (!res.headersSent)
		res.setHeader('X-Powered-By', pkg.name + ' ' + pkg.homepage)
	next()
})



const noCache = nocache()

api.get('/stations/:id/departures', noCache, departures)
api.get('/journeys', noCache, journeys)
api.get('/stations', stations)
github derhuerst / db-rest / api.js View on Github external
const departures = require('./lib/departures')
const journeys = require('./lib/journeys')
const stations = require('./lib/stations')
const allStations = require('./lib/all-stations')



const api = express()
module.exports = api

api.use(hsts({maxAge: 24 * 60 * 60 * 1000}))

morgan.token('id', (req, res) => req.headers['x-identifier'] || shorthash(req.ip))
api.use(morgan(':date[iso] :id :method :url :status :response-time ms'))

const allowed = corser.simpleRequestHeaders.concat(['User-Agent', 'X-Identifier'])
api.use(corser.create({requestHeaders: allowed})) // CORS

api.use(compression())

api.use((req, res, next) => {
	if (!res.headersSent)
		res.setHeader('X-Powered-By', pkg.name + ' ' + pkg.homepage)
	next()
})



const noCache = nocache()

api.get('/stations/:id/departures', noCache, departures)
api.get('/journeys', noCache, journeys)
github local-npm / local-npm / lib / pouchdb-server-lite.js View on Github external
module.exports = function(options) {
    var port = options.pouchPort;
    var directory = path.resolve(options.directory);
    var app = express();
    var logger = new Logger(Logger.getLevel(options.logLevel));

    app.use(util.request(logger));
    app.use(compression());
    app.use(favicon(path.resolve(__dirname, '..', 'dist', 'favicon.ico')));

    app.use(corser.create({
        methods: ['GET', 'HEAD', 'POST', 'PUT', 'DELETE'],
        supportsCredentials: true,
        requestHeaders: corser.simpleRequestHeaders.concat(["Authorization", "Origin", "Referer"])
    }));

    // set up express-pouchdb with the prefix (directory)
    var ScopedPouchDB = PouchDB.defaults({
        prefix: directory + '/'
    });
    var configFile = path.resolve(directory, 'config.json');
    var logFile = path.resolve(directory, 'log.txt');
    // hacky, but there doesn't seem to be any other way to prefix the log file
    fs.writeFileSync(configFile, JSON.stringify({
        log: {
            file: logFile
        }
    }), 'utf-8');
    var pouchDBApp = expressPouchDB({
        configPath: configFile
github LabsRS-Dev / AiEXifCool / server / server.js View on Github external
if (options.getTmpAssetsFilesFn) {
      return options.getTmpAssetsFilesFn(req, res)
    }

    res.emit('next')
  })

  if (options.cors) {
    this.headers['Access-Control-Allow-Origin'] = '*'
    this.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Range'
    if (options.corsHeaders) {
      options.corsHeaders.split(/\s*,\s*/)
          .forEach(function (h) { this.headers['Access-Control-Allow-Headers'] += ', ' + h }, this)
    }
    before.push(nm_corser.create(options.corsHeaders ? {
      requestHeaders: this.headers['Access-Control-Allow-Headers'].split(/\s*,\s*/)
    } : null))
  }

  if (options.robots) {
    before.push(function (req, res) {
      if (req.url === '/robots.txt') {
        res.setHeader('Content-Type', 'text/plain')
        var robots = options.robots === true
          ? 'User-agent: *\nDisallow: /'
          : options.robots.replace(/\\n/, '\n')

        return res.end(robots)
      }

      res.emit('next')
github http-party / http-server / lib / server.js View on Github external
corsHandler(headers, corsHeaders) {
    const corserHeaders = corsHeaders
          ? {requestHeaders: headers['Access-Control-Allow-Headers'].split(/\s*,\s*/)}
          : null;

    return corser.create(corserHeaders);
  }
github pouchdb / pouchdb-server / packages / node_modules / pouchdb-server / lib / cors.js View on Github external
function corsChanged() {
    if (config.get('httpd', 'enable_cors')) {
      var origins = config.get('cors', 'origins');
      if (origins === '*') {
        origins = undefined;
      }
      if (origins) {
        origins = origins.split(', ');
      }

      corsMiddleware = corser.create({
        methods: config.get('cors', 'methods').split(', '),
        supportsCredentials: config.get('cors', 'credentials'),
        requestHeaders: config.get('cors', 'headers').split(', '),
        origins: origins
      });
    } else {
      corsMiddleware = null;
    }
  }
github jamesshore / object_playground / node_modules / http-server / lib / http-server.js View on Github external
before.push(function (req, res) {
    if (options.logFn) {
      options.logFn(req, res);
    }

    res.emit('next');
  });

  if (options.cors) {
    this.headers['Access-Control-Allow-Origin'] = '*';
    this.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Range';
    if (options.corsHeaders) {
      options.corsHeaders.split(/\s*,\s*/)
          .forEach(function (h) { this.headers['Access-Control-Allow-Headers'] += ', ' + h; }, this);
    }
    before.push(corser.create(options.corsHeaders ? {
      requestHeaders: this.headers['Access-Control-Allow-Headers'].split(/\s*,\s*/)
    } : null));
  }

  if (options.robots) {
    before.push(function (req, res) {
      if (req.url === '/robots.txt') {
        res.setHeader('Content-Type', 'text/plain');
        var robots = options.robots === true
          ? 'User-agent: *\nDisallow: /'
          : options.robots.replace(/\\n/, '\n');

        return res.end(robots);
      }

      res.emit('next');
github jamesshore / automatopia / node_modules / http-server / lib / http-server.js View on Github external
before.push(function (req, res) {
    if (options.logFn) {
      options.logFn(req, res);
    }

    res.emit('next');
  });

  if (options.cors) {
    this.headers['Access-Control-Allow-Origin'] = '*';
    this.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Range';
    if (options.corsHeaders) {
      options.corsHeaders.split(/\s*,\s*/)
          .forEach(function (h) { this.headers['Access-Control-Allow-Headers'] += ', ' + h; }, this);
    }
    before.push(corser.create(options.corsHeaders ? {
      requestHeaders: this.headers['Access-Control-Allow-Headers'].split(/\s*,\s*/)
    } : null));
  }

  if (options.robots) {
    before.push(function (req, res) {
      if (req.url === '/robots.txt') {
        res.setHeader('Content-Type', 'text/plain');
        var robots = options.robots === true
          ? 'User-agent: *\nDisallow: /'
          : options.robots.replace(/\\n/, '\n');

        return res.end(robots);
      }

      res.emit('next');

corser

A highly configurable, middleware compatible implementation of CORS.

MIT
Latest version published 8 years ago

Package Health Score

67 / 100
Full package analysis