Explorar el Código

Trying to make CORS work by using a 3rd party dependency

Brendan Abolivier hace 8 años
padre
commit
e1da8e1f86
Firmado por: Brendan Abolivier <contact@brendanabolivier.com> ID clave GPG: 8EF1500759F70623
Se han modificado 2 ficheros con 7 adiciones y 3 borrados
  1. 1
    0
      package.json
  2. 6
    3
      server.js

+ 1
- 0
package.json Ver fichero

@@ -17,6 +17,7 @@
17 17
   },
18 18
   "dependencies": {
19 19
     "body-parser": "1.15.2",
20
+    "cors": "2.8.1",
20 21
     "express": "4.14.0",
21 22
     "node-minify": "1.3.9",
22 23
     "nodemailer": "2.4.2",

+ 6
- 3
server.js Ver fichero

@@ -5,6 +5,7 @@ var settings    = require('./settings');
5 5
 
6 6
 // Web server
7 7
 var bodyParser  = require('body-parser');
8
+var cors        = require('cors');
8 9
 var express     = require('express');
9 10
 var app = express();
10 11
 
@@ -29,11 +30,13 @@ app.use(express.static('front'));
29 30
 // Body parsing
30 31
 app.use(bodyParser.urlencoded({ extended: true }));
31 32
 app.use(bodyParser.json());
32
-
33
-
34 33
 // Allow cross-origin requests.
34
+app.use(cors({
35
+  origin: settings.formUrl,
36
+  optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
37
+}));
38
+
35 39
 app.all('/*', function(req, res, next) {
36
-    res.header('Access-Control-Allow-Origin', settings.formUrl);
37 40
     res.header('Access-Control-Allow-Headers', 'Content-Type')
38 41
     next();
39 42
 });