Node.js使用Express时,对端口对监听有两种方式,笔记如下:
const express = require('express')
const app = express()
var server = app.listen(8080)
app.get('/', function (req, res) {
console.log("GET Reqest");
res.send('Hello GET');
})
const http = require('http')
const https = require('https')
const fs = require('fs')
const express = require('express')
const app = express()
var server = http.createServer(app).listen(8080)
const options = {
key: fs.readFileSync('./key.pem'),
cert: fs.readFileSync('./key-cert.pem')
};
var sserver = https.createServer(options, app).listen(443)
app.get('/', function (req, res) {
console.log("Express GET Reqest");
res.send('Hello GET');
})
server.on("request",function(req,res){
console.log("hello http")
});
sserver.on("request",function(req,res){
console.log("hello https");
});
其中参考key.pem内容如下,放置于工程根目录下:
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCvnCVHnYMIb8LscjwBw7rmh8Cs2I0KaovFB0pMHhupPaknsgFH
AxYkkPgat1A1XSTLM8Li7k2jFU/tBDMPLPRo0h6iiSffavbsp2AoenewagN4nM0A
qRsNW/emPpBKQWPMOy0aJXOsrRvjGlVX0NkaKcmoNHic6abkS/PQoEfm0QIDAQAB
AoGAUN3DoQXciC8HAODd1yYt0+ihhja2RByqPlHMGd8nd7jIQ1pKEkxOjwoe5haS
HfDFd7lfPibko3u18XwkkxFtfnAIMqaIU7672ga+2cGoBr4z/mKgXdxv1/kDy9fN
v2QTyvpBr3HMfCMVrXV78hUUwAsoWgrQ9eLKKfkkqDJdBtECQQDdNr/kFqnXv/gS
jj05DDSrzMo1h3cCPzQ7DPkxH83GyuqYeIhyxeZ6IGGHeZWmyyEOr1ypbUwXe1Qb
d6gnT5ytAkEAyzmNGU5eCYBKYQRes3jMQCVJZPdoYDEWaLDiaYXV6efn67xmtkAp
OuX9RpqUtrDmTOsaoV7mAjlvOz6FD64zNQJBAM4dnV5g34/vhq2Davgkr08EWcTW
m8S0L252ABoNkCaSG1RFup12V/GjCjy4xP/MdNjB/IzBEg0vFIcjTsKSI2ECQGpt
NEBpcwPExK70C+gvPb/mBv6yNl4RtuuSnq+mYDVDtogUvTF6fl0S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