Last active
October 27, 2016 09:34
-
-
Save ovarunendra/48bc596eb84c0644b77805be1d8307d4 to your computer and use it in GitHub Desktop.
How to create an https server?
This file contains hidden or 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
$openssl genrsa -out client-key.pem 2048 | |
$openssl req -new -key client-key.pem -out client.csr | |
$openssl x509 -req -in client.csr -signkey client-key.pem -out client-cert.pem |
This file contains hidden or 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 https = require('https'); | |
var fs = require('fs'); | |
var options = { | |
key: fs.readFileSync('client-key.pem'), | |
cert: fs.readFileSync('client-cert.pem') | |
}; | |
var a = https.createServer(options, function (req, res) { | |
res.writeHead(200); | |
res.end("hello world\n"); | |
}).listen(8000); |
This file contains hidden or 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 http = require('http'); | |
var https = require('https'); | |
var fs = require('fs'); | |
var Canvas = require('canvas') | |
, Image = Canvas.Image | |
, canvas = new Canvas(200, 200) | |
, ctx = canvas.getContext('2d'); | |
var fileUpload = require('express-fileupload'); | |
var options = { | |
key: fs.readFileSync('client-key.pem'), | |
cert: fs.readFileSync('client-cert.pem') | |
}; | |
var express = require('express'); | |
var app = express(); | |
app.use(fileUpload()); | |
var httpServer = http.createServer(app); | |
var httpsServer = https.createServer(options, app); | |
app.get('/files', function(req, res) { | |
console.log("files route") | |
fs.readFile(__dirname + '/user.png', function(err, data) { | |
if (err) throw err; | |
var img = new Canvas.Image; // Create a new Image | |
img.src = data; | |
// Initialiaze a new Canvas with the same dimensions | |
// as the image, and get a 2D drawing context for it. | |
var canvas = new Canvas(400, 300); | |
var ctx = canvas.getContext('2d'); | |
ctx.drawImage(img, 0, 0, 400, 300); | |
//res.write('<html><body>'); | |
//res.write('<img src="' + canvas.toDataURL() + '" />'); | |
//res.write('</body></html>'); | |
res.write(canvas.toDataURL('image/jpeg', 0.6)); | |
res.end(); | |
}); | |
}) | |
app.post('/upload', function(req, res) { | |
var sampleFile, uploadPath; | |
console.log('body'+req.body); | |
if (!req.files) { | |
console.log('No files were uploaded.'); | |
res.send('No files were uploaded.'); | |
return; | |
} | |
sampleFile = req.files['myImage']; | |
uploadPath = __dirname + '/uploadedfiles/' + sampleFile.name; | |
sampleFile.mv(uploadPath, function(err) { | |
console.log(' files were uploaded.'); | |
if (err) { | |
res.status(500).send(err); | |
} | |
else { | |
fs.readFile(uploadPath, function(err, data) { | |
if (err) throw err; | |
var img = new Canvas.Image; // Create a new Image | |
img.src = data; | |
// Initialiaze a new Canvas with the same dimensions | |
// as the image, and get a 2D drawing context for it. | |
var canvas = new Canvas(400, 300); | |
var ctx = canvas.getContext('2d'); | |
ctx.drawImage(img, 0, 0, 400, 300); | |
//res.write('<html><body>'); | |
//res.write('<img src="' + canvas.toDataURL() + '" />'); | |
//res.write('</body></html>'); | |
canvas.toDataURL('image/jpeg', 1, function(err, jpeg){ | |
res.send(jpeg); | |
}); | |
//res.end(); | |
}); | |
//res.send('File uploaded!'); | |
} | |
}); | |
}); | |
httpServer.listen(8080, '0.0.0.0'); | |
httpsServer.listen(8443, '0.0.0.0'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment