Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'corser' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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);
});
};
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();
}
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)
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)
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
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')
corsHandler(headers, corsHeaders) {
const corserHeaders = corsHeaders
? {requestHeaders: headers['Access-Control-Allow-Headers'].split(/\s*,\s*/)}
: null;
return corser.create(corserHeaders);
}
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;
}
}
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');
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');