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:
Related
Im setting up a chat server with socket.io. I followed their tutorial til it came to SSL. I found some explainations here on stack and tutorials in the net, so i came up with the following code.
I replaced my real domain with "my-domain.de"
var app = require('express')();
var fs = require('fs');
var https = require('https');
var io = require('socket.io')(https);
var mysql = require('mysql');
var options = {
key: fs.readFileSync('/etc/letsencrypt/live/my-domain.de/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/my-domain.de/cert.pem'),
ca: fs.readFileSync('/etc/letsencrypt/live/my-domain.de/chain.pem')
};
var serverPort = 8443;
var server = https.createServer(options, app);
server.listen(serverPort, function(){
console.log('listening on *:' + serverPort);
});
io.on('connection', function(socket){
console.log('a user connected');
});
The Server runs, but the Client doesnt recieve the socket.io.js. So i started the server with DEBUG=* -node index.js to see whats happening. It shows the following Error when a client tried to connect:
express:application no routes defined on app +9s
finalhandler default 404 +0ms
The Client looks like this:
<script src="https://my-domain.de:8443/socket.io/socket.io.js"></script>
<script>
$(function () {
var socket = io('https://my-domain.de:8443/');
</script>
After hours of headache i found the solution and it is as simple as always...
I simply had to place var io = require('socket.io')(server); after var server = https.createServer(options, app); So that it can route correctly...
So that my final server code looks like this:
var app = require('express')();
var fs = require('fs');
var https = require('https');
var mysql = require('mysql');
var options = {
key: fs.readFileSync('/etc/letsencrypt/live/my-domain.de/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/my-domain.de/cert.pem'),
ca: fs.readFileSync('/etc/letsencrypt/live/my-domain.de/chain.pem')
};
var serverPort = 8443;
var server = https.createServer(options, app);
var io = require('socket.io')(server); // HERE TO PUT THE DAMN SOCKET.IO!!
server.listen(serverPort, function(){
console.log('listening on *:' + serverPort);
});
io.on('connection', function(socket){
console.log('a user connected');
});
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 have below application successfully started but it doesn't listen on port 8080.
var app = require('../app');
var debug = require('debug')('shanadminpanel:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '8080');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
When I enter url localhost:8080, nothing happened.
You have use http
So you can create server by any of this .
Two way you can create server
by http module
var http = require("http");
var server = http.createServer(function(request, response) {
console.log("server created")
}).listen(80);
And by express you can do this.
by express
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World');
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
I am working in c9.io ide environment, I have written below code in server.js file
var http = require('http');
var path = require('path');
var async = require('async');
var socketio = require('socket.io');
var express = require('express');
var express = require('express');
var app = express();
var router = express();
var server = http.createServer(router);
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
var addr = server.address();
console.log("Server listening at", addr.address + ":" + addr.port);
});
app.use(express.static(__dirname + '/client'));
// respond with "hello world" when a GET request is made to the homepage
app.get('/', function(req, res) {
res.render('index.html');
});
app.get('/about', function (req, res) {
res.send('about');
});
After running node server.js in terminal the message given as
Your code is running at https://nodejs2-mujaffar.c9.io.
Important: use process.env.PORT as the port and process.env.IP as the host in your scripts!
Server listening at 0.0.0.0:8080
But after accessing https://nodejs2-mujaffar.c9.io/ url -- It is not rendering view only displaying message Error: Cannot GET /
What I am doing wrong?
Please help.
You seem to have created two instances of express which may be your problem.
Try changing:
var express = require('express');
var app = express();
var router = express();
var server = http.createServer(router);
to:
var express = require('express');
var app = express();
var server = http.createServer(app);
At the minute, your express app variable is not bound to your http server. You have instead bounded an unused instance called router. But then you have registered your routes to the app variable.
I'm trying to get a nodejs wss-server up and running. For this I'm using the Express4 Framework and the eniaros/ws module.
But sadly I'm not able to get the WSS-Server up and running. The normal WS-Server is working quite fine, but every time I try to connect to my WSS the client gets connected and directly disconnected again.
My certs are self-signed.
My code is shown below. It would be awesome if some one could please help me!
var express = require('express');
var router = express.Router();
var app = express();
var http = require('http');
var https = require('https');
var WebSocket = require('ws').Server;
var path = require('path');
var fs = require('fs');
// Routes
app.use('/', require('./routes/index'));
// Load config files
var db_conf = require('./config/vops_db_config.json');
var server_conf = require('./config/vops_server_config.json');
// TLS-Files
var cert_basepath = './config/certs/';
var tls_key = fs.readFileSync(cert_basepath + server_conf.tls.key_path);
var tls_cert = fs.readFileSync(cert_basepath + server_conf.tls.cert_path);
var https_options = {
ssl: true,
port: 3030,
key : tls_key,
cert: tls_cert
};
// Start all HTTP-Servers
var httpServer = http.createServer(app);
var httpsServer = https.createServer(https_options, app);
httpServer.listen(3000, function () {
console.log('HTTP-Server now listening on port ' + 3000);
});
httpsServer.listen(3030, function(){
console.log('HTTPS-Server now listening on port '+ 3030);
});
// Start WebSocket-Server
var wss = new WebSocket( { server: httpServer });
wss.on('connection', function(ws) {
ws.on('message', function(message) {
console.log('received: %s', message);
});
ws.send('something');
});