Forem

Kauress
Kauress

Posted on

14 3

2 Minute Self Signed SSL Certificate for localhost

This weekend I was tasked with building a login system using passport.js's facebook strategy. Sure it was going to be easy...

Issue was that Facebook dev now enforces 'HTTPS' so you need a self signed SSL certificate for localhost:3000 or whatever port you're using.

Pretty easy.... (till you're in production mode and you realize why your pet has a happier life than you)😂🐶😹⌨️

In Express v.4 The bin directory serves is where you define your startup scripts. The 'www' file contains code to start your express app as a web server. So 'app' is passed as a handler to your web server. If using express generator with "$ npm install express-generator -g" a template with the bin directory and 'www' file is created for you.

In your terminal:

//Note: you will be presented with lots of options. Be sure to fill the Common Name option as localhost

 $ openssl req -nodes -new -x509 -keyout server.key -out server.cert

//convert cert and key to PEM Files

 $ openssl x509 -in server.cert -out cert.pem -outform PEM
 $cat server.cert server.key > key.pem

Enter fullscreen mode Exit fullscreen mode

copy both cert.pem and key.pem in project directory. And then in 'www' within your bin directory, you will need to reference your certificate

/**
 * Module dependencies.
 */

const app = require('../app');
const debug = require('debug')('node-passport-social:server');
const http = require('https');
const fs = require("fs");

/**
 * Get port from environment and store in Express.
 */

const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
 * Create HTTPS server.
 */

const privateKey = fs.readFileSync('./key.pem');
const certificate = fs.readFileSync('./cert.pem');

const server = http.createServer({
  key: privateKey,
  cert: certificate
}, app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

 const bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  const addr = server.address();
 const bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

Enter fullscreen mode Exit fullscreen mode

And then in your browser open https:localhost:3000

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (2)

Collapse
 
weirdmayo profile image
Daniel Mayovsky

Please add javascript code highlighting to the code above.
with

   ` ` `javascript
   ` ` `
Collapse
 
a777med profile image
a777med

I love you

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay