简体   繁体   中英

Node.JS https server for Webhook gets handshake error

The following NodeJS just handles a webhook coming from another source. I tested it out, it works on http port 80 but the source requires https.

When I opened that port and ran this script on 443, testing it with curl gets the following error. Yet I don't think it should require a certificate should it? How would I even solve this?

curl: (35) SSL peer handshake failed, the server most likely requires a client certificate to connect

Here is the script:

var http = require('https')

var server = http.createServer(function(req, res) {
    if (req.method == "POST") {
        var str = ''
        req.on('data', function(data) {
            str += data
        })
        req.on('end', function() {
            var json = JSON.parse(str)
            res.end(json.meta.status)
        })
    }
})
console.log("HTTPS server listening on port 443...")
server.listen(443)

UPDATE:

Here's the latest code. I created a self-signed cert without a passphrase. I get further but I still get an error using curl with the -k option added. I get a certificate verification error without the -k.

Cannot POST /

var https = require('https')
var fs = require('fs')
var express = require('express');

var options = {
    key: fs.readFileSync('./server.key'),
    cert: fs.readFileSync('./server.cert')
}

var app = express()

var server = https.createServer(options, app, function(req, res) {
    if (req.method == "POST") {
        var str = ''
        req.on('data', function(data) {
            str += data
        })
        req.on('end', function() {
            var json = JSON.parse(str)
            res.end(json.meta.status)
        })
    }
})
console.log("HTTPS server listening on port 443...")
server.listen(443)

HTTPS server config always requires SSL certificate. You can generate it using openssl here is in more details . Then for node server use crypto , fs modules. Detailed config is here .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM