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');
});
Related
I have a very simple node setup with only express installed. I pasted this command from here into my Ubuntu 20 bash
openssl req -nodes -new -x509 -keyout server.key -out server.cert
When I run this code:
const express = require('express');
const http = require('http');
const https = require('https');
const fs = require('fs');
const httpPort = 3000;
const httpsPort = 3001;
app = express();
var key = fs.readFileSync(__dirname + '/server.key');
var cert = fs.readFileSync(__dirname + '/server.cert');
var credentials = {
key: key,
cert: cert,
};
//GET home route
app.get('/', (req, res) => {
res.send('Hello World.');
});
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
httpServer.listen(httpPort, () => {
console.log('Http server listing on port : ' + httpPort);
});
httpsServer.listen(httpsPort, () => {
console.log('Https server listing on port : ' + httpsPort);
});
This results in an error when I access localhost:3001
What am I doing wrong?
Thanks in advance for your time!
My mistake was that I typed localhost:3001 in my browser search which made me request http://localhost:3001.
Typing https://localhost:3001 worked.
I'm using session.setProxy to route every request through my basic express server.
I want to load a webpage and replace the page content with my own content.
If I load a http url the content from the express server is loaded but if I use https the app is just blank.
Here's the main.js of my electron app:
const electron = require('electron')
const { app, BrowserWindow } = electron
var win;
app.on('ready', initwindow);
function initwindow() {
win = new BrowserWindow({show:true});
win.webContents.session.setProxy({
proxyRules: 'https://xxx',
}, () => {
win.loadURL('https://www.google.com/');
});
}
Here's my express server:
const fs = require('fs');
const http = require('http');
const https = require('https');
const express = require('express');
const app = express();
const credentials = {
key: fs.readFileSync('/etc/letsencrypt/live/xxx/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/xxx/fullchain.pem'),
};
app.get('/', function(req, res) {
res.send('Hello world')
})
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
httpServer.listen(80, () => {
console.log('HTTP Server running on port 80');
});
httpsServer.listen(443, () => {
console.log('HTTPS Server running on port 443');
});
For example, if I use win.loadURL('http://www.google.com/'); "Hello world" is displayed, but if I use win.loadURL('https://www.google.com/'); the electron app is just white.
Is there something missing from my server or is what i'm trying to do no possible?
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.
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);