Code Monkey home page Code Monkey logo

express-cors's Introduction

How to handle CORS (Cross-Origin Resource Sharing) in Express.JS Application

This repository contains all code to handle CORS in ExpressJS Application

Nowadays when we develop web application often we create different web services that will handle our http requests. Those requests may come from different domain or mobile devices. In this case you need to enable CORS request (Cross-Origin Resource Sharing)

In general if you make a request from web application to a server in different domain and the CORS is not enabled you will get something like this

alt disabled cors

For more information about CORS please see the following resources:

HTTP access control (CORS)

Cross-Origin Resource Sharing

Enable CORS

In ExpressJS application you need to use a middleware to make possible CORS requests https://www.npmjs.com/package/cors

In this repo there are two servers one that start on http://127.0.0.1:5000 and is the api server and other that start on http://127.0.0.1:5001 and serve simple web application that makes request on the previous server.

Installation

To install and test the code please follow the following steps:

  • Clone or fork the repository
  • Go to the local repository folder and digit
npm install
  • Once all requested modules have been installed open two different shell screen, point on the project folder and in the first digit
npm start
  • In the second shell screen digit
npm run client

Now you can see two different server started now open browser and point to http://localhost:5001/ and enjoy the simple web application that test your api server making http reqiest to it.

Web Application to test CORS request to the API server

alt enabled cors

I created this repository for all of you have trouble in handle logs in ExpressJS Application. Feel free to send me feedback and issue to improve the example.

Nicola Del Gobbo

https://github.com/NickNaso/

https://www.npmjs.com/~nicknaso

https://twitter.com/NickNaso

express-cors's People

Contributors

nicknaso avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

mariraja508

express-cors's Issues

npm install failing

npm ERR! path .....forest-saasgroup/node_modules/express-cors
npm ERR! code EISGIT
npm ERR! git ....forest-saasgroup/node_modules/express-cors: Appears to be a git repo or submodule.
npm ERR! git     ....forest-saasgroup/node_modules/express-cors
npm ERR! git Refusing to remove it. Update manually,
npm ERR! git or move it out of the way first.

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.