Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hapi-cors

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-cors

Enables cors for a hapijs app based on config.

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

hapi-cors

Enables cors for a hapijs app based on config.

Default Options:

{
    origins: ['*'],
    allowCredentials: 'true',
    exposeHeaders: ['content-type', 'content-length'],
    maxAge: 600,
    methods: ['POST, GET, OPTIONS'],
    headers: ['Accept', 'Content-Type', 'Authorization']
}

Using an origins array:

'use strict'

const Hapi = require('hapi');

const server = Hapi.server({
    host: 'localhost',
    port: 8000
});

server.route({
    method: 'GET',
    path: '/hello',
    handler: function(request, h) {
        return h.response({greeting: "Hello there!"});
    }
});

const start = async function() {
    try {
        await server.register({
            plugin: require('hapi-cors'),
            options: {
                origins: ['http://localhost:3000']
            }
        })

        await server.start();
    } catch(err) {
        console.log(err);
        process.exit(1);
    }
};

start();

Using a function:

'use strict'

const Hapi = require('hapi');

const server = Hapi.server({
    host: 'localhost',
    port: 8000
});

server.route({
    method: 'GET',
    path: '/hello',
    handler: function(request, h) {
        return h.response({greeting: "Hello there!"});
    }
});

const checkOrigin = (origin) => {
    if(origin === 'http://localhost:3000'){
        return true
    }else{
        return false
    }
}

const start = async function() {
    try {
        await server.register({
            plugin: require('hapi-cors'),
            options: {
                checkOrigin: checkOrigin
            }
        })

        await server.start();
    } catch(err) {
        console.log(err);
        process.exit(1);
    }
};

start();

Keywords

FAQs

Package last updated on 02 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc