// 參考文章:
// https://nodejs.org/api/https.html
// http://stackoverflow.com/questions/11744975/enabling-https-on-express-js
// npm install express
// exps.js
var https = require("https");
var fs = require("fs");
var url = require("url");
var path = require("path");
var express = require("express");
var options = {
key: fs.readFileSync('mypass.key'),
cert: fs.readFileSync('mypass.crt')
};
function getDOC(req,res) {
var uri = url.parse(req.url).pathname;
var filename = path.join(process.cwd()+"/www", uri); // add doc root: /www
fs.access(filename, function(accessErr) {
if(accessErr) { res.send("File not found"); return; }
if (fs.statSync(filename).isDirectory()) filename +='/mybaby.htm'; // default html document
fs.readFile(filename, "binary", function(err, file) {
if(err) { res.send(err); return; }
res.writeHead(200);
res.write(file, "binary");
res.end();
});
})
}
var app = express();
app.get('/*',getDOC);
var server=https.createServer(options, app);
server.listen(4443, function() {
console.log("HTTPS sever started at port %s",server.address().port);
});
// 再寫個 Makefilet 產生 ssl 所需要的檔案及目錄,方便使用及執行.
// Makefile
run: genkey exps.js
@js exps.js
genkey: mypass.crt www
mypass.key:
openssl genrsa -out mypass.key 2048
mypass.csr: mypass.key
openssl req -new -key mypass.key -out mypass.csr
mypass.crt: mypass.csr mypass.key
openssl x509 -req -in mypass.csr -signkey mypass.key -out mypass.crt
www:
mkdir www
clean:
rm -f mypass.*
沒有留言:
張貼留言