0

Trying to create a secure server for nodeJs application using https.js NPM module.

app.js:

var http = require('https'),
    fs = require('fs');

var cert = {
    key:     fs.readFileSync('/opt/cert/server.key.pem'),
    cert:    fs.readFileSync('/opt/cert/server.cert.pem')
};

console.log(cert);

var server = http.createServer(cert, function (req, res) {
    console.log("I am the listener!");
    res.write('Hello World!'); //write a response to the client
    res.end(); //end the response
});

server.on('error', function (e) {
    // Handle your error here
    console.log(e);
});

server.listen(3005, function(e) {
    console.log("bambam");
    console.log(e);
});

Tried locally and on remote server. The port is listening since the browser doesn't say that connection refused but it does say that there's no response.

4
  • share the error log Commented Sep 11, 2018 at 18:48
  • try res.send(), I don't like other res functions. Commented Sep 11, 2018 at 18:57
  • @mehta-rohan no error log; just my own console.log lines. the server.on('error'... never runs. Commented Sep 11, 2018 at 19:02
  • Your code looks ok, what is the endpoint URL you're hitting from the browser? Commented Sep 11, 2018 at 19:26

2 Answers 2

1

Not sure what you are doing wrong, might be you forget to pass certificate authority.

Here's my app.js for secure server and it's working good.

app.js:

  /*jshint multistr: true, node: true, esversion: 6, undef: true, unused: true, varstmt: true*/
  "use strict";

  // NPM Modules
  const bodyParser                  = require('body-parser'),
        CORS                        = require('cors'),
        express                     = require('express'),
        FS                          = require('fs'),
        HTTPS                       = require('https'),
        path                        = require('path'),
  // Internal Modules
        console                     = require('./config/logger').log,
        router                      = require('./router');

  const app                         = express();

  // Cross-Origin Resource Sharing
  app.use(CORS());

  // configure the app to use bodyParser() to extract body from request.
  // parse urlencoded types to JSON
  app.use(bodyParser.urlencoded({
  extended: true
  }));

  // parse various different custom JSON types as JSON
  app.use(bodyParser.json({ type: 'application/*+json' }));

  // parse some custom thing into a Buffer
  app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }));

  // parse an HTML body into a string
  app.use(bodyParser.text({ type: 'text/html' }));

  // Setup views directory, file type and public filder.
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.static(path.join(__dirname, 'public')));

  app.use(function(req, res, next) {
        if (req.secure) {
        next();
        } else {
        res.redirect('https://' + req.headers.host + req.url);
        }
  });

  router(app);

  const port = process.env.PORT || 3000;

  console.info('server listening at https://127.0.0.1 over port: ', port);

  const key                     = FS.readFileSync( './config/encryption/private.key' ),
        cert                    = FS.readFileSync( './config/encryption/mydomain.crt' ),
        ca                      = FS.readFileSync( './config/encryption/mydomain.crt' );

  HTTPS.createServer( {key, cert, ca}, app).listen(3000);
Sign up to request clarification or add additional context in comments.

Comments

0

try like this:

var http = require('http');
http.createServer(function (req, res) {
    console.log("I am the listener!");
    res.write('Hello World!'); //write a response to the client
    res.end(); //end the response
}).listen(3005);

1 Comment

Wakinov is looking for secure server https, not a normal http.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.