We are going to create a photo sharing service where the user is able to upload pictures and like them.
We will be using a simple MVC, so our file structure will be:
/controllers
/users.js
/photos.js
/likes.js
/models
/users.js # this will be half-mocked up since there's no time
# or we'll use auth0 if possible
/views
/index.jade
/login.jade
/photo.jade
/.gitignore
/app.js
/package.json
/routes.js
For this, first we set up our app.js:
// Parse the .env config
require('dotenv').config();
var express = require('express');
var bodyparser = require('body-parser');
var app = express();
app.set('view engine', 'pug');
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(process.env.PORT || 3000, function () {
console.log('Example app listening on port 3000!');
});
Then in our controllers/photos.js:
var formidable = require('formidable');
var cloudinary = require('cloudinary');
cloudinary.config({
cloud_name: process.env.cloud,
api_key: process.env.key,
api_secret: process.env.secret
});
exports.upload = function(req, res, next){
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
if (err) next(err);
cloudinary.uploader.upload(files.image.path, function(result) {
// Let's force https in the image:
var image = result.url.replace('http://', 'https://');
res.json({ error: false, image: image });
});
});
});
Auth
We will follow Auth0 tutorial.