I want to handle the POST request data in NODEJS using HTTPS. Something like below,
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");
});
But turns out, this only works for HTTP request. This will not give any error but it wont receive the data to be received in '/xyz'.
Thanks.
也许您可以使用反向代理之类的方法来更好地解决此问题?
Maybe you need to check your certificates. I have checked this code through my certificates and they are working with https. I m putting code here:
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");
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.