Unable to run on https in nodejs server - javascript

I have created following certificate and key(self-signed). When I run the app, the browser loads with Not secure and https is striked off. It should run on https right when the key and cert is available?
Code:
var express = require('express');
var app = express();
var fs = require('fs');
var http = require('http');
var https = require('https');
var request = require('request');
var rp = require('request-promise');
var port = process.env.PORT || 5026;
app.set('port', (port));
app.use('/', express.static(__dirname + '/public_html'));
var options = {
key: fs.readFileSync('./server.key', 'utf8'),
cert: fs.readFileSync('./server.crt', 'utf8')
};
http.createServer(app).listen(8080);
https.createServer(options, app).listen(8443);
There's no error in console though. I have never worked with ssl before. What am I missing here?
Update: Added CA Certificate and private key
But now I get this error:
Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
at Error (native)
at Object.createSecureContext (_tls_common.js:65:17)
at Server (_tls_wrap.js:758:25)
I am using windows OS. But why this error? How should I fix this?

Related

How do i fix this error: localhost/:1 Failed to load resource: the server responded with a status of 404 (Not Found), Cannot Get /

I'm creating an online multiplayer .io game similar to https://diep.io/ or https://agar.io/ and I'm in the process of setting up a server this is my code,
var path = require('path');
var http = require('http');
var express = require('express');
var socketIO = require('socket.io');
var publicPath = path.join(__dirname, '../client');
var port = process.env.PORT || 2000;
var app = express();
var server = http.createServer(app);
var io = socketIO(server);
app.use(express.static(publicPath));
server.listen(port, function () {
console.log('Server stared on port ' + port);
});
When I start the server and put "localhost:2000" into the search bar this is what comes up,
I'm kinda new to this so if you could make the answers simple to understand then I'd really appreciate it.
Double check your paths. Your code will work as written if you have a folder structure like so:
/server/index.js
/client/index.html
$ node server/index.js
The static middleware by default will serve files which match html, html.
So if you have index.html, you can make a request to /, and if you have hello.html, you can hit it at /hello.
See here for more options: http://expressjs.com/en/resources/middleware/serve-static.html
Note: this middleware is what Express.static uses, so no need to install that package, just look at examples for serveStatic() and pass those to Express' version.

https://localhost returned ERR_SSL_PROTOCOL_ERROR (secure connection)

I am using Node and express.js to build my server and I setup my https server (essentially) like this:
const privateKey = fs.readFileSync('./credentials/rippal.key', 'utf8');
const certificate = fs.readFileSync('./credentials/rippal.crt', 'utf8');
const credentials = {key: privateKey, cert: certificate};
const app = express();
setup(app, config);
let httpsServer = https.createServer(credentials, app)
httpsServer.listen(3333, function () {
console.log("Server listening on port 3333...\n");
});
with self-signed certificates, it still works to http://localhost:3333 but when I tried to access https://localhost:3333, Chrome told me
This site can’t provide a secure connection
localhost sent an invalid response.
ERR_SSL_PROTOCOL_ERROR
how should I access https://localhost:3333?
Thanks!

Why Apache server start automatically everyday and it stops the nodeJs?

I am working in MEAN STACK application, I face one issue in a live site.
In my live web site, I have stop Apache server and run nodeJs by using pm2.
Once nodeJs started by pm2, my site running very well, but after every next day, Apache server automatically starts and my nodeJs site stop.
After stopping of Apache server the nodeJS works fine.
app.js
"use strict";
var logger = require("./config/log");
var express = require("express");
var https = require('https');
var http = require("http");
var fs = require('fs');
var app = express();
var exec = require('child_process').exec;
var config, hostName, sslOptions, httpServer, callSocketDir;
/**
* #description all process variables
*/
require("./config/vars")(app, fs);
config = require("./config/config.js")(app, express);
callSocketDir = './socket';
sslOptions = {
key: fs.readFileSync(global.hzConfig.privateKey),
cert: fs.readFileSync(global.hzConfig.certificate),
passphrase: global.hzConfig.passPhrase
};
httpServer = https.createServer(sslOptions, app).listen(global.hzConfig.port,function (req, res) {
logger.log("info", 'my site is listening on ssl port ' + global.hzConfig.port + ", proccess id is " + process.pid + '!');
initEmailServer();
});
require(callSocketDir)(app, httpServer);
Stop apache server
/usr/local/apache/bin/apachectl stop
Start nodejs server
pm2 start httpsServer.js
dependencies
"express" => "version" : "4.13.4",
"nodeJs" => "version" : "v7.4.0",
"https" => "version" : "^1.0.0"
Please give me a proper guideline for this issue.
Basically the Apache server and nodejs server might be on the same port
for example
if node js server running on port 80 and your Apache server to port is also running port 80
try and set one of the server to port 8080

using https with express io

So I am new to express and io but I had a server running fine for webRTC but now there is a deprecated method in webRTC that only runs on https so I tried to create an https server but it starts and then immediately exits. I cannot figure out what is wrong and I do not get any errors. Also I am using an aws ec2 to run the express io server. Maybe someone can spot where in my syntax/implementation I am going wrong.
Note I have been googling around for the past half hour and cannot figure it out
Here is the code:
var connect = require('connect');
var https = require('https');
var fs = require('fs');
var express = require('express.io');
var app = express();
//app.http().io();
var PORT = 443;
var options = {
key: fs.readFileSync('../server.key'),
cert: fs.readFileSync('../server.crt')
};
app.https(options).io();
//var app = https.createServer(options, app1);
console.log('server started on port ' + PORT);
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
res.render('index.ejs');
});
app.listen(PORT);
app.io.route('ready', function(req) {
req.io.join(req.data.chat_room);
req.io.join(req.data.signal_room);
app.io.room(req.data).broadcast('announce', {
message: 'New client in the ' + req.data + ' room.'
})
})
Update
I am putting a bounty on this because I would like someone to provide me with a complete answer on setting up the server for production.
You need to add a rule for port 443 in a Security Group for your instance.
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html might help.

node.js / Https with express

I try to use express in node.js with https.
Here the relevant code for this parts:
var express = require("express");
var app = express();
var https = require('https');
var privateKey = fs.readFileSync('./sslcert/mykey.key', 'utf8');
var certificate = fs.readFileSync('./sslcert/mssl.crt', 'utf8');
var credentials = {key: privateKey, cert: certificate};
https.createServer(credentials, app).listen(5008, function(res){
console.log("Listen to: " + port)
});
In the console, it prints Listen to: 5008, but I don't see that my server get any request, even though I sent some.
When I use express without the ssl extension, everything works fine.
EDIT:
Here the code which works OK with port 5008, but without the ssl part:
var express = require("express");
var app = express();
app.listen(5008, function () {
console.log("Listening on " + port);
});
What I do wrong?
You have to specify https://, otherwise the browser/client will not know to use SSL when connecting, no matter what port is used (the default of 443 or otherwise).

Categories

Resources