Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fs = require "fs"
- express = require "express"
- http = express.createServer()
- httpsOptions =
- key: fs.readFileSync('key.pem'),
- cert: fs.readFileSync('cert.pem')
- https = express.createServer(httpsOptions)
- http.all '*', (req, res) ->
- console.log "HTTP: #{req.url}"
- res.redirect "https://#{req.headers["host"]}#{req.url}"
- http.error (error, req, res, next) ->
- console.log "HTTP error #{error}, #{req.url}"
- https.error (error, req, res, next) ->
- console.log "HTTPS error #{error}, #{req.url}"
- https.all '*', (req, res) ->
- console.log "HTTPS: #{req.url}"
- res.send("Hello, World!")
- http.listen(80)
- https.listen(443)
- $ curl --include --silent http://localhost/foo
- HTTP/1.1 302 Moved Temporarily
- X-Powered-By: Express
- Content-Type: text/html
- Location: https://localhost/foo
- Connection: keep-alive
- Transfer-Encoding: chunked
- <p>Moved Temporarily. Redirecting to <a href="https://localhost/foo">https://localhost/foo</a></p>
- $ curl --include --silent --insecure https://localhost:443/foo
- HTTP/1.1 200 OK
- X-Powered-By: Express
- Content-Type: text/html; charset=utf-8
- Content-Length: 13
- Connection: keep-alive
- Hello, World!%
- curl --include --silent --location --insecure 'http://localhost/foo?bar=bux'
- HTTP/1.1 302 Moved Temporarily
- X-Powered-By: Express
- Content-Type: text/html
- Location: https://localhost/foo?bar=bux
- Connection: keep-alive
- Transfer-Encoding: chunked
- HTTP/1.1 200 OK
- X-Powered-By: Express
- Content-Type: text/html; charset=utf-8
- Content-Length: 13
- Connection: keep-alive
- Hello, World!%
- curl --insecure https://localhost:80/foo
- curl: (35) Unknown SSL protocol error in connection to localhost:80
- curl http://localhost:443/foo
- curl: (52) Empty reply from server
- var https = require('https');
- var fs = require('fs');
- var sslkey = fs.readFileSync('ssl-key.pem');
- var sslcert = fs.readFileSync('ssl-cert.pem')
- var options = {
- key: sslkey,
- cert: sslcert
- };
- express.createServer(options);
- app.use(function(req, res, next) {
- var schema = req.headers['x-forwarded-proto'];
- if (schema === 'https') {
- // Already https; don't do anything special.
- next();
- }
- else {
- // Redirect to https.
- res.redirect('https://' + req.headers.host + req.url);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement