I've successfully created the SSL certificate and the key for my website via letsencrypt.org . I uploaded them in a folder on the server. I get no errors when I start the server.
But when I try to load the website in the browser using https://, I get the "refused to connect" error.
Here is the code I am using to create the https server:
'use strict';
const express = require('express');
const fs = require('fs');
const http= require('http');
const https= require('https');
const path = require('path');
const PRIVATEKEY_PATH = path.join(__dirname, '../ssl/server.key');
const CERTIFICATE_PATH = path.join(__dirname, '../ssl/server.crt');
var privateKey = fs.readFileSync(PRIVATEKEY_PATH, 'utf8');
var certificate = fs.readFileSync(CERTIFICATE_PATH, 'utf8');
var credentials = {key: privateKey, cert: certificate};
// Constants
const PORT = process.env.PORT || 8080;
const HOST = '0.0.0.0';
const CLIENT_BUILD_PATH = path.join(__dirname, '../../300meter.ch/build');
// App
const app = express();
// Static files
app.use(express.static(CLIENT_BUILD_PATH));
//ssh
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
// All remaining requests return the React app, so it can handle routing.
app.get('*', function(request, response) {
response.sendFile(path.join(CLIENT_BUILD_PATH, 'index.html'));
});
httpServer.listen(PORT, HOST);
httpsServer.listen(8443, HOST);
If somebody could help me, that would be awesome. Thanks!
When you specify https://example.com, the client connects to port 443. You do not have your SSL server running on port 443. Your server is running on port 8443.
You will need to specify https://example.com:8443/ in the URL.
Related
I can get bitbucket to run at localhost:7990 just fine however I wanted to secure it with a SSL and also remove the port from the address. When I attempt to do this with the code below and my node server I just simply get the 'CANNOT GET /' error if I goto https://example.com. Even though the bitbucket app is running.
// Dependencies
const fs = require('fs');
const path = require('path');
const http = require('http');
const https = require('https');
const express = require('express');
const express_force_ssl = require('express-force-ssl');
const app = express();
// Certificate
const privateKey = fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem', 'utf8');
const certificate = fs.readFileSync('/etc/letsencrypt/live/example.com/fullchain.pem', 'utf8');
const ca = fs.readFileSync('/etc/letsencrypt/live/example.com/chain.pem', 'utf8');
const credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
app.use(express_force_ssl);
// Load main application
app.use(express.static(path.join(__dirname, '../../var/www/bitbucket/')));
// Starting both http & https servers
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
httpServer.listen(80, (req,res) => {
console.log('HTTPS Server running on port 80');
});
httpsServer.listen(443, (req, res) => {
console.log('HTTPS Server running on port 443');
});
So I am successful when deploying my React app to production server. I am using Express for deploying. Here is my code.
const express = require('express')
const path = require('path')
const port = process.env.PORT || 80
const app = express()
app.use(express.static(__dirname + '/build'))
app.get('*', function (request, response){
response.sendFile(path.resolve(__dirname, 'build', 'index.html'))
})
app.listen(port)
I would use CertBot for SSL certs and deploy this as HTTPS server. I really don't know how to approach this problem.
Let's say for example the CertBot generated the SSL certificate and SSL certificate key to the following locations:
/etc/letsencrypt/live/example.org/fullchain.pem
/etc/letsencrypt/live/example.org/privkey.pem
After you have generated your certificate and key, you'll need to utilize the https module directly.
You'll first need to export your Express app:
const express = require('express')
const path = require('path')
const port = process.env.PORT || 80
const app = express()
app.use(express.static(__dirname + '/build'))
app.get('*', function (request, response){
response.sendFile(path.resolve(__dirname, 'build', 'index.html'))
})
module.export = app
Then simply import your Express app and hook it up with the https module:
const https = require('https');
const fs = require('fs');
const app = require('./app.js');
const server = https.createServer({
key: fs.readFileSync('/etc/letsencrypt/live/example.org/fullchain.pem', 'utf8'),
cert: fs.readFileSync('/etc/letsencrypt/live/example.org/privkey.pem', 'utf8')
}, app);
server.listen(3000);
Note this essentially what ./bin/www does for you except I've adapted it to use https instead of http module.
See What does "./bin/www" do in Express 4.x?
I try to configure my node server to SSL, my node server and php both work on the same instance. node is start without error but when i send a request though the socket or web url, it will crashed. only show
segmentation Fault
my node version is 6.9.4
here my Node js script
var https = require('https');
var http = require('http');
var fs = require('fs');
var app = require('express')();
var f_root = 'path_to_ssl';
var options = {
key: fs.readFileSync(f_root+'ssl.myserver.key'),
cert: fs.readFileSync(f_root+'ssl.myserver.pem')
};
app.get("/", function(request, response){
console.log(" Hello World");
});
var httpsserver = https.createServer(options, app);
httpsserver.listen(3001);
Finally found the issue
issue was the .pem file. i use the .crt instead of .pem file
here the update
var https = require('https');
var http = require('http');
var fs = require('fs');
var app = require('express')();
var f_root = 'path_to_ssl';
var options = {
key: fs.readFileSync(f_root+'ssl.myserver.key'),
// this is the issue
cert: fs.readFileSync(f_root+'ssl.myserver.crt')
};
app.get("/", function(request, response){
console.log(" Hello World");
});
var httpsserver = https.createServer(options, app);
httpsserver.listen(3001);
This is the code that was provided in the example:
'use strict';
var server = require('./app');
var port = process.env.PORT || process.env.VCAP_APP_PORT || 3000;
server.listen(port, function() {
console.log('Server running on port: %d', port);
});
But when using https instead of server it is not working well with IBM Watson conversation code.
The below is the code I used:
var https = require('https');
var fs = require('fs');
var server = require('./app');
var port = process.env.PORT || process.env.VCAP_APP_PORT || 3000;
var options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
var a = https.createServer(options, function (req, res) {
server.listen(port, function() {
console.log('Server running on port: %d', port);
});
}).listen(port);
In the case, Express API doc spells this out pretty clearly.
And this article can help too.
You can create a HTTPS in node.js with:
var express = require('express'); //express for it
var server = require('./app');
var https = require('https');
var http = require('http');
var fs = require('fs');
var port = process.env.PORT || process.env.VCAP_APP_PORT || 443; //example
// from the Node.js HTTPS documentation, almost the same your code.
var options = {
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.cert')
};
// Create a service (the app object is just a callback).
var app = express();
// Create an HTTP service.
http.createServer(app).listen(80); //you can put the port too.
// Create an HTTPS service identical to the HTTP service.
https.createServer(options, app).listen(port);
The Express documentation show this:
I have four pages inside a folder "public". I want to open that pages with HTTPS beacause getusermedia() no longer works in HTTP connections. This is my code:
var express = require("express");
var https = require('https');
var BinaryServer = require('binaryjs').BinaryServer;
var fs = require('fs');
var options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
var app = express();
var server = https.createServer(options, app);//.listen(9000);
app.use(express.static('public'));
app.listen(9000);
But the pages doesn't open in HTTPS (just open in HTTP). Before I check the certificate I want to be sure that everything is ok with the code.
You shouldnt use app.listen, as this is still only the "regular" express app (aka http) I believe.
This should point you in the right direction (taken from the API docs # http://expressjs.com/en/api.html)
Edit:
Added in how your code should look:
var express = require("express");
var https = require('https');
var BinaryServer = require('binaryjs').BinaryServer;
var fs = require('fs');
var options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
var app = express();
app.use(express.static('public'));
https.createServer(options, app).listen(9000);