Browse Source

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

Brendan Abolivier 8 years ago
parent
commit
e1da8e1f86
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
2 changed files with 7 additions and 3 deletions
  1. 1
    0
      package.json
  2. 6
    3
      server.js

+ 1
- 0
package.json View File

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

+ 6
- 3
server.js View File

5
 
5
 
6
 // Web server
6
 // Web server
7
 var bodyParser  = require('body-parser');
7
 var bodyParser  = require('body-parser');
8
+var cors        = require('cors');
8
 var express     = require('express');
9
 var express     = require('express');
9
 var app = express();
10
 var app = express();
10
 
11
 
29
 // Body parsing
30
 // Body parsing
30
 app.use(bodyParser.urlencoded({ extended: true }));
31
 app.use(bodyParser.urlencoded({ extended: true }));
31
 app.use(bodyParser.json());
32
 app.use(bodyParser.json());
32
-
33
-
34
 // Allow cross-origin requests.
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
 app.all('/*', function(req, res, next) {
39
 app.all('/*', function(req, res, next) {
36
-    res.header('Access-Control-Allow-Origin', settings.formUrl);
37
     res.header('Access-Control-Allow-Headers', 'Content-Type')
40
     res.header('Access-Control-Allow-Headers', 'Content-Type')
38
     next();
41
     next();
39
 });
42
 });