Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. fs = require "fs"
  2. express = require "express"
  3. http = express.createServer()
  4.  
  5. httpsOptions =
  6. key: fs.readFileSync('key.pem'),
  7. cert: fs.readFileSync('cert.pem')
  8. https = express.createServer(httpsOptions)
  9.  
  10. http.all '*', (req, res) ->
  11. console.log "HTTP: #{req.url}"
  12. res.redirect "https://#{req.headers["host"]}#{req.url}"
  13.  
  14. http.error (error, req, res, next) ->
  15. console.log "HTTP error #{error}, #{req.url}"
  16.  
  17. https.error (error, req, res, next) ->
  18. console.log "HTTPS error #{error}, #{req.url}"
  19.  
  20. https.all '*', (req, res) ->
  21. console.log "HTTPS: #{req.url}"
  22. res.send("Hello, World!")
  23.  
  24. http.listen(80)
  25. https.listen(443)
  26.  
  27. $ curl --include --silent http://localhost/foo
  28. HTTP/1.1 302 Moved Temporarily
  29. X-Powered-By: Express
  30. Content-Type: text/html
  31. Location: https://localhost/foo
  32. Connection: keep-alive
  33. Transfer-Encoding: chunked
  34.  
  35. <p>Moved Temporarily. Redirecting to <a href="https://localhost/foo">https://localhost/foo</a></p>
  36.  
  37. $ curl --include --silent --insecure https://localhost:443/foo
  38. HTTP/1.1 200 OK
  39. X-Powered-By: Express
  40. Content-Type: text/html; charset=utf-8
  41. Content-Length: 13
  42. Connection: keep-alive
  43.  
  44. Hello, World!%
  45.  
  46. curl --include --silent --location --insecure 'http://localhost/foo?bar=bux'
  47. HTTP/1.1 302 Moved Temporarily
  48. X-Powered-By: Express
  49. Content-Type: text/html
  50. Location: https://localhost/foo?bar=bux
  51. Connection: keep-alive
  52. Transfer-Encoding: chunked
  53.  
  54. HTTP/1.1 200 OK
  55. X-Powered-By: Express
  56. Content-Type: text/html; charset=utf-8
  57. Content-Length: 13
  58. Connection: keep-alive
  59.  
  60. Hello, World!%
  61.  
  62. curl --insecure https://localhost:80/foo
  63. curl: (35) Unknown SSL protocol error in connection to localhost:80
  64.  
  65. curl http://localhost:443/foo
  66. curl: (52) Empty reply from server
  67.  
  68. var https = require('https');
  69. var fs = require('fs');
  70.  
  71. var sslkey = fs.readFileSync('ssl-key.pem');
  72. var sslcert = fs.readFileSync('ssl-cert.pem')
  73.  
  74. var options = {
  75. key: sslkey,
  76. cert: sslcert
  77. };
  78.  
  79. express.createServer(options);
  80.  
  81. app.use(function(req, res, next) {
  82. var schema = req.headers['x-forwarded-proto'];
  83.  
  84. if (schema === 'https') {
  85. // Already https; don't do anything special.
  86. next();
  87. }
  88. else {
  89. // Redirect to https.
  90. res.redirect('https://' + req.headers.host + req.url);
  91. }
  92. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement