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);
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');
});
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.
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 wrote an HTTPS NodeJS server (with a self signed certificate) where I want to receive and process POST requests from cURL CLI.
My server:
var https = require('https');
var fs = require('fs');
var config = require('./conf.json');
var g3updater_kernel = require('./g3updater_kernel');
var express = require('express');
var app = express();
var privateKey = fs.readFileSync(config.security_repository+'/CA/rootCA.key', 'utf8');
var certificate = fs.readFileSync(config.security_repository+'/CA/rootCA.pem', 'utf8');
var httpsOptions = {
key: privateKey,
cert: certificate
}
app.post('/', function(req, res){
console.log(req.query);
});
https.createServer(httpsOptions, app).listen(443);
cURL command I use:
curl -d M=1 https://localhost/ -k
The problem is that I receive an empty query. console.log(req.query) displays:
{}
am I missing something in the query?
In order to parse the queries in your post request you're going to want to use body-parser.
In Express 4, there was conscious decision made to move bodyParser and CookieParser to separate modules.
In order to access your queries, you're code should look something like:
var https = require('https');
var fs = require('fs');
var config = require('./conf.json');
var g3updater_kernel = require('./g3updater_kernel');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var privateKey = fs.readFileSync(config.security_repository+'/CA/rootCA.key', 'utf8');
var certificate = fs.readFileSync(config.security_repository+'/CA/rootCA.pem', 'utf8');
var httpsOptions = {
key: privateKey,
cert: certificate
}
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
app.post('/', function(req, res){
console.log(req.body);
});
https.createServer(httpsOptions, app).listen(443);
Note that they will be held in req.body instead of req.query.
Happy Coding!