ERR_SSL_PROTOCOL_ERROR browser error message while running a https server in nodejs express - node.js

I have a number of nodejs applications running on Express using the code below. They all run fine using code similar to the following:
fs = require 'fs'
https = require 'https'
express = require 'express'
server_port = 3000
keys_dir = 'keys/' server_options = {
key : fs.readFileSync(keys_dir + 'privatekey.pem'),
cert : fs.readFileSync(keys_dir + 'certificate.pem') } app = express.createServer(server_options)
app.listen server_port
console.log "HTTPS Server started on port #{server_port}"
However, when trying to create a new application using this code I see a ERR_SSL_PROTOCOL_ERROR when starting the https server. Any idea what is causing this problem?

I discovered that was caused when moving from express 2.5.8 to express 3 - specifically 3.0.0beta4. When creating a new project the version pulled from npm had changed to the version 3 series. Even though the module is marked as "beta" when you run express --version this version is what is installed now when running npm install express. The details of the changes are outlined here.
To solve this for my case I used the following code:
const fs = require("fs");
const https = require("https");
const express = require("express");
const keysDir = "keys/";
const options = {
key : fs.readFileSync(keysDir + "privatekey.pem"),
ca : fs.readFileSync(keysDir + "certrequest.csr"),
cert : fs.readFileSync(keysDir + "certificate.pem")
};
const app = express();
https.createServer(options, app).listen(3000);

Related

React not recognizing Express

In my jsx file, I have the following:
var express = require('express');
var myExpress = express();
var http = require('http');
var app = http.createServer(myExpress);
var { Server } = require("socket.io");
var myio = new Server(app);
However, the browser says "Uncaught TypeError: express is not a function"
I have tried importing express with an import statement, as well as making my project a module in my package.json. What is weird is that when I use the same code in a regular js file in the same folder, it works perfectly well. This code was the code in every single one of the tutorials, so I am at a loss. Thank you.
Express is node framework you can't use it in react.
I think you need https://v5.reactrouter.com/web/guides/quick-start

node.js Websocket raspberry pi 4B ERR_CONNECTION_REFUSED

During this last days I'been trying to run a node.js web through raspberry pi 4 connected to a private network as a server running in ubuntu server. At first time it worked in my laptop with Windows 10.
The steps are:
Install:
yarn install
Run ReactJS Client:
yarn start
Run NodeJS Server (in a separate terminal window):
node server.js
And the code in the file server.js is:
const http = require('https');
const socketIO = require('socket.io');
const DeepSpeech = require('deepspeech');
const VAD = require('node-vad');
let DEEPSPEECH_MODEL = __dirname + '/deepspeech-0.6.0-models'; // path to deepspeech english model directory
let SILENCE_THRESHOLD = 200; // how many milliseconds of inactivity before processing the audio
const SERVER_PORT = 4000; // websocket server port
// const VAD_MODE = VAD.Mode.NORMAL;
// const VAD_MODE = VAD.Mode.LOW_BITRATE;
// const VAD_MODE = VAD.Mode.AGGRESSIVE;
const VAD_MODE = VAD.Mode.VERY_AGGRESSIVE;
const vad = new VAD(VAD_MODE);
etc.
And I get this error:
polling-xhr.js:267
GET http://localhost:4000/socket.io/?EIO=3&transport=polling&t=N3VNHWo
net::ERR_CONNECTION_REFUSED
Any idea?

parse-Server cloudCode with nodejs

I am using parse-server and I want to use nodejs with cloudCode as in the example below.
This is the example:
Adding nodejs to Parse
here is the example code from the link
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var ParseCloud = require('parse-cloud-express');
var Parse = ParseCloud.Parse;
var app = express();
// Host static files from public/
app.use(express.static(__dirname + '/public'));
// Define a Cloud Code function:
Parse.Cloud.define('hello', function(req, res) {
res.success('Hello from Cloud Code on Node.');
});
// Mount the Cloud Code routes on the main Express app at /webhooks/
// The cloud function above will be available at /webhooks/function_hello
app.use('/webhooks', ParseCloud.app);
// Launch the HTTP server
var port = process.env.PORT || 80;
var server = http.createServer(app);
server.listen(port, function() {
console.log('Cloud Code on Node running on port ' + port + '.');
});
console.log(process.env.PORT);
I have imported all the required modules, but still, when I run the server and try to go to the link "127.0.0.1/webhooks/function_hello" I get back Cannot GET /webhooks/function_hello
Any advise?
*OUTPUT when i run the script *
undefined
Cloud Code on Node running on port 80.
UPDATE it seems that with parse's shutdown that they have changed support status for cloudcode which affects integrating it with NodeJs
Had the same issue. GET doesn't work here. You need to make a POST request, and then you'll get {"success":"Hello from Cloud Code on Node."}
Please make sure you are running the right script with node SCRIPT_NAME
It appears your express server is set to port 5000.
See: var port = process.env.PORT || 5000;
Change your URL to http://127.0.0.1:5000/webhooks/function_hello or localhost:5000/webhooks/function_hello and it should appear
If you want to run on the default port (80) you will need to run with sudo for your script and make the following change to the code.
var port = process.env.PORT || 80;
Add a folder to your directory named public. Inside that folder place a file named index.html. Type Hello World in that file, save it. Restart your server. See if you can open http://127.0.0.1/.

Unexpected error using Node.js's Express with vhosts

Here's the error:
Here's the code:
runapps.js:
var express = require('express');
var app = express();
var first = require('./apps/ghost_gforce/index.js').app;
var second = require('./apps/ghost_blog2/index.js').app;
app
.use(express.vhost('gforce/main', first))
.use(express.vhost('gforce/blog2', second))
.listen(80);
running this straight from node like below works perfectly:
node apps\ghost_gforce\index.js
UPDATE:: ran:
npm install
within the node app directory, so nothing should be missing.
Thanks in advance!
In your test you are calling a relative path:
apps\ghost_gforce\index.js
But in your code you are using a path based on the root of your filesystem:
/apps/ghost_blog2/index.js
Try this:
var first = require('./apps/ghost_gforce/index.js').app;
var second = require('./apps/ghost_blog2/index.js').app;

unable to execute server.js program using express framework on node.js

While trying to execute server.js program I am getting the following error:
var app = express();
Type error: object is not a function
at object.<anonymous>
even tried re installing and changing the version of express to
npm install
npm uninstall express
npm install express#2.5.9
but it resulted in new error
fqdn = ~req.url.indexof(' ://')
I use windows and i am working on node.js version 0.8.4.
If you're using Express < 3.0, the return value of require('express'); is not a function; you'll need to create a server the old way.
Express 2.x
var express = require('express');
var server = express.createServer();
Express 3.x
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app);
What does
> require('express').version;
'3.0.0rc2'
return?
As you can see it does return 3.0.0rc2? Does yours really return 2.5.9. if it does you use like Brandon said 2.x section. If it returns 3.x you use his 3.x section.

Resources