Last active
December 21, 2015 15:59
-
-
Save andrewjmead/6330312 to your computer and use it in GitHub Desktop.
CORS with self-signed certificate. Post:
http://andrewjmead.calepin.co/angularjsexpress-preflight-options-request-canceled.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<!-- I ran with php -S localhost:3007 --> | |
<html lang="en-US"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
<script src="./angular.js"></script> | |
<script type="text/javascript"> | |
var app = angular.module('app', []); | |
app.run(function ($http) { | |
console.log('***** running'); | |
$http.get('https://localhost:3006/api') | |
.success(function (data, status, headers, config) { | |
console.log('***** success'); | |
console.dir(arguments); | |
}) | |
.error(function (data, status, headers, config) { | |
console.log('***** error'); | |
console.dir(arguments); | |
}); | |
$http.post('https://localhost:3006/api?name=mike', {}, { | |
headers: { | |
"X-Token": "123abc" | |
} | |
}) | |
.success(function (data, status, headers, config) { | |
console.log('***** post success'); | |
console.dir(arguments); | |
}) | |
.error(function (data, status, headers, config) { | |
console.log('***** post error'); | |
console.dir(arguments); | |
}); | |
}); | |
</script> | |
</head> | |
<body ng-app="app"> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'), | |
fs = require('fs'), | |
app = express(), | |
options = { | |
key: fs.readFileSync(__dirname + '/ssl/server.key').toString(), | |
cert: fs.readFileSync(__dirname + '/ssl/server.crt').toString() | |
}, | |
server = require('https').Server(options, app), | |
//server = require('http').Server(app), | |
PORT = 3006; | |
app.use(function (req, res, next) { | |
console.log('***** New Request *****'); | |
console.log(req.method + ' ' + req.path); | |
res.header('Access-Control-Allow-Origin', '*'); | |
if (req.method === 'OPTIONS') { | |
res.header('Access-Control-Allow-Methods', 'OPTIONS,GET,PUT,POST,PATCH,DELETE'); | |
res.header('Access-Control-Allow-Headers', req.headers['access-control-request-headers']); | |
return res.send(200); | |
} | |
next(); | |
}); | |
app.get('/api', function (req, res) { | |
return res.send(200, { | |
"name": "andrew mead" | |
}); | |
}); | |
app.post('/api', function (req, res) { | |
return res.send(200, { | |
"name": req.query.name, | |
"token": req.headers['x-token'] | |
}); | |
}); | |
server.listen(PORT, function () { | |
console.log('PRODUCTION SERVER'); | |
console.log("Express server listening on port " + app.get('port')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment