我想使用HTTPS处理NODEJS中的POST请求数据。像下面这样
var express = require('express')
var bodyParser = require('body-parser')
var https = require('https');
// Create a new instance of express
var app = express()
// Tell express to use the body-parser middleware and to not parse extended bodies
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }))
//private key and certificate for secure connection (https)
var options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem'),
requestCert: false,
rejectUnauthorized: false,
};
var httpsApp = https.createServer(options,app);
// Route that receives a POST request to /sms
httpsApp._events.request.post('/xyz', function (req, res) {
-----
some code
-----
})
httpsApp.listen(5004, function(){
console.log("listening on port 5004");
logger.info("listening on port 5004");
});
但是事实证明,这仅适用于HTTP请求。这不会产生任何错误,但不会接收要在'/ xyz'中接收的数据。
谢谢。
答案 0 :(得分:0)
也许您可以使用反向代理之类的方法来更好地解决此问题?
答案 1 :(得分:0)
也许您需要检查证书。 我已经通过证书检查了此代码,并且它们正在使用https。 我将代码放在这里:
var express = require('express')
var bodyParser = require('body-parser')
var https = require('https');
var fs = require('fs')
// Create a new instance of express
var app = express()
// Tell express to use the body-parser middleware and to not parse extended bodies
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }))
//private key and certificate for secure connection (https)
var options = {
key: fs.readFileSync('./certificates/server.key'),
cert: fs.readFileSync('./certificates/server.crt'),
requestCert: false,
rejectUnauthorized: false,
};
var httpsApp = https.createServer(options,app);
// Route that receives a POST request to /sms
httpsApp._events.request.get('/xyz', function (req, res) {
console.log("Works")
res.send("working")
})
httpsApp.listen(5004, function(){
console.log("listening on port 5004");
});