RaiBnod RaiBnod - 5 months ago 14
Node.js Question

NodeJS error on established https connection

I want to establish https request to my api and i have written following lines of code in NodeJS function:

function getUsers() {
/**
* HOW TO Make an HTTP Call - GET
*/
// options for GET
var optionsget = {
host: 'localhost', // here only the domain name
// (no http/https !)
port: 3000,
path: '/api/users', // the rest of the url with parameters if needed
method: 'GET' // do GET
};

console.info('Options prepared:');
console.info(optionsget);
console.info('Do the GET call');

// do the GET request
var reqGet = https.request(optionsget, function (res) {
console.log('Requested');
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function (d) {
console.info('GET result:\n');
process.stdout.write(d);
console.info('\n\nCall completed');
});

});

reqGet.end();
reqGet.on('error', function (e) {
console.error('Last error:' + e);
});
}


Server side code:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mysql = require("mysql");
var md5 = require('MD5');
var rest = require("./REST.js");

var index = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

function REST() {
var self = this;
self.connectMysql();
}

REST.prototype.connectMysql = function () {
var self = this;
var pool = mysql.createPool({
connectionLimit: 100,
host: 'localhost',
user: 'root',
password: 'root',
database: 'oes_api',
debug: false
});
pool.getConnection(function (err, connection) {
if (err) {
self.stop(err);
} else {
self.configureExpress(connection);
}
});
};

REST.prototype.configureExpress = function (connection) {
var self = this;
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
var router = express.Router();
app.use('/api', router);
app.use('/', index);
app.use('/users', users);
var rest_router = new rest(router, connection, md5);
self.startServer();
};

REST.prototype.startServer = function () {
app.listen(3000, function () {
console.log("All right! I am alive at Port 3000.");
});
};

REST.prototype.stop = function (err) {
console.log("ISSUE WITH MYSQL \n" + err);
process.exit(1);
};

new REST();


But https connection is not establishing saying Last error:Error: socket hang up.

Could you please help me out to get ride out from this error.

Answer

It looks like you can create your server using https. Take a look at this page in the Node Documentation for doing so.

Example

const https = require('https');

https.createServer(options, (req, res) => {
  res.writeHead(200);
  res.end('hello world\n');
}).listen(8000);
Comments