Skip to content

Commit

Permalink
Added file upload example
Browse files Browse the repository at this point in the history
  • Loading branch information
sahat committed Feb 9, 2016
1 parent ca6c1cb commit 40a68c8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 8 deletions.
21 changes: 14 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Module dependencies.
*/
var _ = require('lodash');
var express = require('express');
var cookieParser = require('cookie-parser');
var compress = require('compression');
Expand All @@ -19,7 +20,8 @@ var mongoose = require('mongoose');
var passport = require('passport');
var expressValidator = require('express-validator');
var sass = require('node-sass-middleware');
var _ = require('lodash');
var multer = require('multer');
var upload = multer({ dest: path.join(__dirname, 'uploads') });

/**
* Load environment variables from .env file, where API keys and passwords are configured.
Expand Down Expand Up @@ -86,11 +88,15 @@ app.use(session({
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(lusca({
csrf: true,
xframe: 'SAMEORIGIN',
xssProtection: true
}));
app.use(function(req, res, next) {
if (req.path === '/api/upload') {
next();
} else {
lusca.csrf()(req, res, next);
}
});
app.use(lusca.xframe('SAMEORIGIN'));
app.use(lusca.xssProtection(true));
app.use(function(req, res, next) {
res.locals.user = req.user;
next();
Expand All @@ -103,7 +109,6 @@ app.use(function(req, res, next) {
});
app.use(express.static(path.join(__dirname, 'public'), { maxAge: 31557600000 }));


/**
* Primary app routes.
*/
Expand Down Expand Up @@ -157,6 +162,8 @@ app.get('/api/paypal/cancel', apiController.getPayPalCancel);
app.get('/api/lob', apiController.getLob);
app.get('/api/bitgo', apiController.getBitGo);
app.post('/api/bitgo', apiController.postBitGo);
app.get('/api/upload', apiController.getFileUpload);
app.post('/api/upload', upload.single('myFile'), apiController.postFileUpload);

/**
* OAuth authentication routes. (Sign in)
Expand Down
11 changes: 11 additions & 0 deletions controllers/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -851,3 +851,14 @@ exports.postBitGo = function(req, res, next) {
return res.redirect('/api/bitgo');
}
};

exports.getFileUpload = function(req, res, next) {
res.render('api/upload', {
title: 'File Upload'
});
};

exports.postFileUpload = function(req, res, next) {
req.flash('success', { msg: 'File was uploaded successfully.'});
res.redirect('/api/upload');
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"method-override": "^2.3.5",
"mongoose": "^4.4.2",
"morgan": "^1.6.1",
"multer": "^1.1.0",
"node-foursquare": "^0.3.0",
"node-linkedin": "^0.5.3",
"node-sass-middleware": "^0.9.7",
Expand Down
6 changes: 6 additions & 0 deletions views/api/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,9 @@ block content
.panel-body
img(src='http://i.imgur.com/v753soI.png', height=40)
| BitGo
.col-sm-4
a(href='/api/upload', style='color: #1565c0')
.panel.panel-default(style='background-color: #fff')
.panel-body
img(src='http://i.imgur.com/UPTzIdC.png', height=40)
| File Upload
25 changes: 25 additions & 0 deletions views/api/upload.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
extends ../layout

block content
.page-header
h2
i.fa.fa-upload
| File Upload

.btn-group.btn-group-justified
a.btn.btn-success(href='https://github.com/expressjs/multer', target='_blank')
i.fa.fa-book
| Multer Documentation
a.btn.btn-success(href='http://codepen.io/search/pens?q=custom+file+upload&limit=all&type=type-pens', target='_blank')
i.fa.fa-laptop
| Customize File Upload

h3 File Upload Form
.row
.col-sm-6
p All files will be uploaded to "/uploads" directory.
form(role='form', enctype='multipart/form-data', method='POST')
.form-group
label.control-label File Input
input(type='file', name='myFile')
button.btn.btn-primary(type='submit') Submit
2 changes: 1 addition & 1 deletion views/contact.jade
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ block content
.col-sm-offset-2.col-sm-8
button.btn.btn-primary(type='submit')
i.fa.fa-envelope
| Send
| Send

0 comments on commit 40a68c8

Please sign in to comment.