Hello one or two months ago I asked for help on this site and several colleagues offered their time (@schoen, @ rg305, etc)
I was working with windows creating an application entirely with Node.js in which I intend to login from facebook.
Well, after trying it for days, I did not get it, so I decided to switch to Ubuntu.
I have created my application from scratch and reach the point of login with faceebock, which I am not allowed by its new HTTPS policy.
The application is running with Express and I have seen several things but I can not perform the complex operations.
I just want to tell me the steps to follow to get my url (michaelgram.test) through https.
I have to tell you that I am new to this topic, that’s why I was not able to get it with Windows, I hope to get it now.
I have searched other unsuccessful questions on this site. In my previous question they also show me certbot, but I do not find Ubuntu 18.04 from where I work.
I show you the code, the same as the previous time.
Sorry for the syntax, I must use the translator.
Thank you.
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(’/logout’, function (req, res) {
req.logout()
res.redirect(’/’)
});
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(’/whoami’, function (req, res) {
if (req.isAuthenticated()) {
return res.json(req.user)
}
res.json({ auth: false })
})
app.get(’/api/pictures’, function (req, res, next) {
client.listPictures(function (err, pictures) {
if (err) return res.send([]);
res.send(pictures)
})
})
app.post(’/api/pictures’, ensureAuth, function (req, res) {
upload(req, res, function (err) {
if (err) {
return res.status(500).send(Error uploading file: ${err.message})
}
let user = req.user
let token = req.user.token
let username = req.user.username
let src = req.file.location
client.savePicture({
src: src,
userId: username,
user: {
username: username,
avatar: user.avatar,
name: user.name
}
}, token, function (err, img) {
if (err) {
return res.status(500).send(err.message)
}
res.send(File uploaded: ${req.file.location}
);
})
})
})
app.get(’/api/user/:username’, (req, res) => {
var username = req.params.username;
client.getUser(username, function (err, user) {
if (err) return res.status(404).send({ error: 'user not found '})
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’);
})