使用Express搭建HTTPs加密服务器

1.生成证书文件

生成私钥key文件:

openssl genrsa 1024 > /文件路径/private.pem

通过私钥文件生成CSR证书签名:

openssl req -new -key /文件路径/private.pem -out /文件路径/csr.pem

通过私钥文件和CSR证书签名生成证书文件:

openssl x509 -req -days 365 -in /文件路径/csr.pem -signkey /文件路径/private.pem -out /文件路径/file.crt

2.修改启动文件

var fs = require('fs');
var options = {
  key: fs.readFileSync('./文件路径/private.pem'),
  cert: fs.readFileSync('./文件路径/file.crt')
};

var https = require('https');
var express = require('express');
var app = express();
app.use(express.static('public'));

app.get('/about',function(req,res){
  console.log(req.url);
  res.send('My name is chenYy');
});
app.get('/about/:kkkk',function(req,res){
  console.log(req.url);
  res.send(req.params);
});
app.get('/about/:kkkk/:ppp',function(req,res){
  console.log(req.url);
  res.send(req.params);
});
app.get('/chenyy/:ooo?',function(req,res){
  console.log(req.params,req.url);
  //用end必须为string或Buffer
  //res.end(req.params.ooo);
  res.send(req.params);
});
app.get('/from/:name/to/:age',function(req,res){
  console.log(req.url,req.params);
  res.json(req.params);
});

//app.set('views','../views');
app.set('view engine','jade');

app.get('/downloads',function(req,res){
  res.send(req.ip);
});
app.use(function(req,res){  
  console.log(req.url,req.params);  
  res.writeHead(200,{'Content-Type':'text/plain'});  
  res.end('Hello World!');
});
var server = https.createServer(options,app);
server.listen(8080);


你可能感兴趣的:(使用Express搭建HTTPs加密服务器)