I'm pretty much following this tutorial (all other tutorials I found look the same)
http://www.hacksparrow.com/express-js-https.html
My code is as follows:
// dependencies
var express = require('express')
, https = require('https')
, fs = require('fs');
var privateKey = fs.readFileSync('./ssl/rp-key.pem').toString();
var certificate = fs.readFileSync('./ssl/rp-cert.pem').toString();
var app = express.createServer({
key : privateKey
, cert : certificate
});
...
// start server
https.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
The app starts fine after sudo node app
Express server listening on port 443
Now when I curl
curl https://localhost/
I get
curl: (35) Unknown SSL protocol error in connection to localhost:443
Any ideas?
Since Express 3.x, which is now being published via npm, the "app()"-Application Function changed. There is an migration info on https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x . None of the express 2.x SSL tutorials will work anymore. The correct Code for express 3.x is:
// dependencies
var express = require('express')
, https = require('https')
, fs = require('fs');
var privateKey = fs.readFileSync('./ssl/rp-key.pem').toString();
var certificate = fs.readFileSync('./ssl/rp-cert.pem').toString();
var options = {
key : privateKey
, cert : certificate
}
var app = express();
...
// start server
https.createServer(options,app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
Related
I have made a node server with with express and sockets io. It is a simple chat application that would be an addon, to my existing project on my website.
I am having this issue where I am unable to connect to the server from the client to be able to use the chat application.
Error: polling-xhr.js:202 GET https://128.127.106.17:6379/socket.io/?EIO=4&transport=polling&t=Nixndgr net::ERR_CONNECTION_TIMED_OUT
SERVER
const fs = require("fs");
const path = require('path');
const express = require('express');
const app = express();
const http = require('http');
const options = {
key: fs.readFileSync(PATH_TO_KEY),
cert: fs.readFileSync(PATH_TO_CERT),
}
const server = http.createServer(app);
const cookie = require('cookie');
const { Server } = require("socket.io");
const io = new Server(server);
CLIENT
var socket = io(HOSTNAME, {'transports': ['websocket', 'polling']});
With these settings it works perfectly fine on my local environment, but with the actually website I have trouble getting it connected.
The few things I have attempted
I tried adding the ssl options , "const server = http.createServer(options, app);"
I played around with CORS as well and different settings.
Found this https://docs.cpanel.net/knowledge-base/web-services/how-to-install-a-node.js-application/
And I found that the server was running and I would get the hello world but still not able to connect to it and starting to get stumped and running out of resources/possible solutions.
Currently, Express server using HTTPS communication and Socket.io are using the same port 443. However, it's necessary to apply the third-party project to the web server, so it's necessary to use the WebSocket package together.
I found the article below to find a way to use the same port 443 for Express, Socket.io, and WebSocket respectively, but I don't know if this is correct.
Reference article URL : Go to page
app.js
const express = require('express');
... Some register middleware
module.exports = app;
bin/www
const app = require('../app');
const https = require('https');
...
const server = https.createServer(options, app);
const io = require('socket.io)(server);
io.on('connection', (socket) => {
....
});
server.listen(443, '0.0.0.0');
I've made a server that works with http just fine. The server was up and I was able to connect to it with Chrome and Postman. When I switch the server to https, the server is up, but I can't connect to it with Chrome and Postman. the ssl keys were sign by certbot.
server.js
const https = require('https');
const app = require(__dirname+'/app');
const fs = require('fs');
const port = 80;
const options = {
cert: fs.readFileSync("ssl/v2/fullchain.pem"),
key: fs.readFileSync("ssl/v2/privkey.pem")
}
https.createServer(options, app).listen(port);
console.log(port);
app.js
const express = require('express');
const morgan = require('morgan');
const body_parser = require('body-parser');
const homepage = require(__dirname+'/routes/homepage');
const user = require(__dirname+'/routes/user');
const test = require(__dirname+'/routes/test');
const table = require(__dirname+'/routes/table');
const catalog = require(__dirname+'/routes/catalog');
const cart = require(__dirname+'/routes/cart');
const payment = require(__dirname+'/routes/payment');
const app = express();
app.use(morgan("dev"));
app.use(body_parser.json());
app.use(body_parser.urlencoded({extended: true}));
app.use("/", homepage);
app.use("/user", user);
app.use("/test", test);;
app.use("/table", table);
app.use("/catalog", catalog);
app.use("/cart", cart);
pp.use("/payment", payment);
module.exports = app;
... the server is up, but I can't connect to it with Chrome and Postman
It is not clear from your description how exactly you are trying to connect to the server but I assume that you'll try a simple https://example.com/.
const port = 80;
...
https.createServer(options, app).listen(port);
But based on your code you are trying to use HTTPS on the port reserved for plain HTTP (80) instead of using the default port for HTTPS (443). Thus, https://example.com/ will not work since this will try to use port 443 and you would need to explicitly specify a different port with https://example.com:80/. But the better option would of course to use the default port for HTTPS in the first place in your code, i.e. 443 instead of 80.
When i dont get any proper answer so i asked this question that i created a chat app for my site using socket.io and node.js my site is on https server it's not working it's giving me error
GET https://example.com:3000/socket.io/?EIO=3&transport=polling&t=1507034613131-2 net::ERR_INSECURE_RESPONSE
CODE IS HERE
var app = require('express')();
var fs = require('fs');
var path = require('path');
var forceSsl = require('express-force-ssl');
app.use(forceSsl);
var options = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
};
var server = require('https').createServer(options, app).listen(3000,function(){
console.log("Https server started on port 3000");
});
var io = require('socket.io').listen(server);
i find above code for https authentication so how can i get these files i have already installed ssl.
i think you can check in your cpanel there is ssl manager which is cpanel service so you can get from there after that you can use in your https nodejs service
var app, certificate, credentials, express, fs, http, httpServer, https, httpsServer, privateKey;
fs = require('fs');
http = require('http');
https = require('https');
privateKey = fs.readFileSync('key.pem', 'utf8');
console.log(privateKey);
certificate = fs.readFileSync('cert.pem', 'utf8');
console.log(certificate);
credentials = {
key: privateKey,
cert: certificate
};
express = require('express');
app = express();
httpServer = http.createServer(app);
httpsServer = https.createServer(credentials, app);
httpServer.listen(80);
httpsServer.listen(443);
I am on OS X and I have confirmed nothing else is listening on 80 and 443. I run this as sudo and when I go http://127.0.0.1, it works. However, when I go to https://127.0.0.1, I get not found.
What am I doing incorrect?
To enable your app to listen for both http and https on ports 80 and 443 respectively, do the following
Create an express app:
var express = require('express');
var app = express();
The app returned by express() is a JavaScript function. It can be be passed to Node’s HTTP servers as a callback to handle requests. This makes it easy to provide both HTTP and HTTPS versions of your app using the same code base.
You can do so as follows:
var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
var app = express();
var options = {
key: fs.readFileSync('/path/to/key.pem'),
cert: fs.readFileSync('/path/to/cert.pem')
};
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);
For complete detail see the doc
add the following line of code:
app.listen(443);
Also, try getting rid of all of the http module, as express handles most of that for you. Look at the beginning Hello World for Express, http://expressjs.com/starter/hello-world.html and look for the part where it handles the port.