How do I configure an SSL certificate for an application that runs in Express?

0

How to set up an SSL certificate for an application that runs in Express?

I'm trying to configure the Express server I created, to pass the SSL certificate and go from http to https.

I have read the Express documentation, but I can not find the solution. They have proposed things to me like Lets Encrypt, but it does not support Node.js. I do not know if I should modify the hosts file, which I have already modified to run the application, or what I have to do. I've seen a form, but it only works on the Unix system. I show the way I set up the server file, if they can help me, it took three days looking for ways to do it to no avail. The ones I've seen do not support Node.js. Thanks

  

Quote   I edit again:   Forgive me, forget to say that my goal is to create the certificate for an application on which you can register on Facebook and tried the methods that my colleagues kindly offered but did not work thanks to the new Facebook policy.   If you have another idea, my domain would be michaelgram.test   Thank you and forgive the inconvenience, for not doing the question well.   

let express = require('express');
let aws = require('aws-sdk');
let multer = require('multer');
let multerS3 = require('multer-s3');
let ext = require('file-extension');
let cookieParser = require('cookie-parser');
let bodyParser = require('body-parser');
let expressSession = require('express-session');
let passport = require('passport');
let michaelgram = require('michaelgram-client');
let auth = require('./auth')
let config = require('./config');
let port = process.env.PORT || 5050;

let client = michaelgram.createClient(config.client);

let s3 = new aws.S3({
  accessKeyId: config.aws.accessKey,
  secretAccessKey: config.aws.secretKey
});

let storage = multerS3({
  s3: s3,
  bucket: 'michaelgram',
  acl: 'public-read',
  metadata: function (req, file, cb) {
    cb(null, { fieldName: file.fieldname })
  },
  key: function (req, file, cb) {
    cb(null, +Date.now() + '.' + ext(file.originalname))
  }
});


let upload = multer({ storage: storage }).single('picture');

let app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(expressSession({
  secret: config.secret,
  resave: false,
  saveUninitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())
app.set('view engine', 'pug');
app.use(express.static('public'));

passport.use(auth.localStrategy);
passport.use(auth.facebookStrategy);
passport.deserializeUser(auth.deserializeUser);
passport.serializeUser(auth.serializeUser);

app.get('/', function (req, res) {
  res.render('index', { title: 'Michaelgram' });
})

app.get('/signup', function (req, res) {
  res.render('index', { title: 'Michaelgram - Signup' });
})

app.post('/signup', function (req, res) {
  let user = req.body;
  client.saveUser(user, function (err, usr) {
    if (err) return res.status(500).send(err.message)
    debugger
    res.redirect('/signin');
  });
});

app.get('/signin', function (req, res) {
  res.render('index', { title: 'Michaelgram - Signin' });
})

app.post('/login', passport.authenticate('local', {
  successRedirect: '/',
  failureRedirect: '/signin'
}));

app.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email' }));

app.get('/auth/facebook/callback', passport.authenticate('facebook', {
  successRedirect: '/',
  failureRedirect: '/signin'
}));

function ensureAuth (req, res, next) {
  if (req.isAuthenticated()) {
    return next()
  }

  res.status(401).send({ error: 'not authenticated' })
}

app.get('/api/pictures', function (req, res, next) {
  let pictures = [ ];

  setTimeout(function () {
    res.send(pictures);
  }, 2000)
});

app.post('/api/pictures', ensureAuth,function (req, res) {
  upload(req, res, function (err) {
    if (err) {
      return res.send(500, "Error uploading file");
    }
    res.send('File uploaded');
  })
})

app.get('/api/user/:username', (req, res) => {
  const user = {
    username: 'miguelito',
    avatar: '',
    pictures: [  ]
  }

  res.send(user);
})

app.get('/:username', function (req, res) {
  res.render('index', { title: 'Michaelgram - ${req.params.username}' });
})

app.get('/:username/:id', function (req, res) {
  res.render('index', { title: 'Michaelgram - ${req.params.username}' });
})

app.listen(port, function (err) {
  if (err) return console.log('Hubo un error'), process.exit(1);

  console.log('Michaelgram escuchando en el puerto 5050');
})
    
asked by anonymous 25.07.2018 / 17:34

2 answers

2

I have an example that uses both http and https. From version 3 of express you can use the following code:

var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey  = fs.readFileSync('keys/key.key', 'utf8');
var certificate = fs.readFileSync('keys/cert.crt', 'utf8');


var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();

// your express configuration here
app.get('/', function(req,res) {
    res.send('hello');
});

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

httpServer.listen(8080, function () {
    console.log("JSON Server is running on  http://localhost:" + 8080);
});
httpsServer.listen(8443, function () {
    console.log("JSON Server is running on  https://localhost:" + 8443);
});

You only need key.key and cert.crt to run on your machine.

I hope I have helped

    
25.07.2018 / 20:20
1

This video author teaches you to deploy an application node with ssl and nginx: link

p>

It has the blog of the guy with the commands to copy and paste, and together the video link: link

Explaining what you are going to do:

Install and configure the node, your db and your app normally;

Will install certbot;

As certbot does not recognize the node, you will have to use something it knows, in this case Nginx link

It acts as a load balancer and reverse proxy for requests, and it's best to work with certbot.

For Nginx you will send the requests on port 80 to your app on port 443 that is with SSL, in the links it teaches you to take an A + in the SSL configuration.

    
25.07.2018 / 17:37