Create & run secure (https) nodejs express app with self-signed certificates - Segmentation fault

こ雲淡風輕ζ 提交于 2019-12-06 14:12:32

I have found a way to solve/simply this.

make-certs.sh

#!/bin/bash

FQDN=`hostname`
rm server.key server.crt
openssl genrsa -out server.key 2048
openssl req -nodes -newkey rsa:2048 -keyout server.key -out server.csr -subj "/C=GB/ST=Street/L=City/O=Organisation/OU=Authority/CN=${FQDN}"
openssl x509 -req -days 1024 -in server.csr -signkey server.key -out server.crt
rm server.csr

api-server.js

// Import libraries
var express = require('express');
var server = express();
var bodyParser = require('body-parser')
var https = require('https');
var fs = require('fs');

// Server setting
var port = process.env.PORT || 8080;

// Register body-parser
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: true }));

// Configure router
var router = express.Router();
server.use('/api/v1', router);

// Create https server & run
https.createServer({
    key: fs.readFileSync('server.key'),
    cert: fs.readFileSync('server.crt')
}, server).listen(port, function() {
    console.log('API Server Started On Port %d', port);
});

// Register routes
router.get('/', function(req, res) {
    res.json({ success: true });
});

This now works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!