Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upload form #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,25 @@ var express = require('express')

var app = express();
var env = process.env.NODE_ENV || 'development';
var config = require('./config/config')[env];

//Bootstrap db connection
mongoose.connect('mongodb://localhost/learning');
mongoose.connect('mongodb://localhost/qalc');
require('./models/user');
require('./models/leaseRequest');
require('./models/loanRequest');
require('./models/leaseResponse');
require('./models/loanResponse');
require('./models/request');
require('./models/commonRequest');

app.use(flash());
var User = mongoose.model("User");
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

app.configure(function(){
app.set('port', process.env.PORT || 3002);
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('title', 'Qalc');
Expand Down
40 changes: 40 additions & 0 deletions controllers/upload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var mongoose = require('mongoose');

// create file schema for mongo
/*var Schema = mongoose.Schema;
var FileSchema = new Schema({
name: {type: String, required: true, unique: true}
});
var File = mongoose.model('File', FileSchema);*/

exports.uploadForm = function(req, res) {
res.render('upload_form');
};

exports.upload = function(req, res) {
req.form.on('end', function(){
var filename = req.files.file.path.split('\\');
filename = path.basename(filename.pop());
var file_data = {
name: filename
};
var file = new File(file_data);
file.save(function (error, data) {
if (error) {
res.json(error);
} else {
console.log("Added New File");
res.statusCode = 201;
res.send();
res.render('upload_form');
}
});
});
};

exports.viewFiles = function(req, res) {
File.find({}, function(error, data) {
//res.json(data);
res.render('data', {data: data, title: 'Test'});
});
};
86 changes: 48 additions & 38 deletions routes.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,53 @@
module.exports = function(app){

/*Controllers setup
* */
var index = require('./controllers/index');
var passport = require('passport');
var users = require('./controllers/users');
var requests = require('./controllers/requests');
var upload = require('./controllers/upload');

/* app.get('/', index.index);
app.get('/admin',ensureAuthenticated , admin.main);
*/

app.get('/', index.index);
app.get('/qalcSelect', index.qalcSelect);
app.get('/login', index.login); app.get('/myQalcs', index.myQalcs);
app.get('/offers', index.offers);
app.get('/bankOffer', index.bankOffer);

var index = require('./controllers/index');
var passport = require('passport');
var users = require('./controllers/users');
var admin = require('./controllers/admin');

/* app.get('/', index.index);
app.get('/admin',ensureAuthenticated , admin.main);
*/

app.get('/users/', users.list);
app.get('/api/users', users.jsonlist);
app.get('/users/:uid', users.findById);
app.post('/users/add', users.add);
app.post('/users/remove', users.remove);
app.post('/users/update/:uid', users.update);
app.get('/login', users.login);
// app.post('/login', passport.authenticate('local'), users.auth);
app.post('/login', passport.authenticate('local', {failureRedirect: '/login'}), users.auth);
app.get('/api', function (req,res) {
var obj = {
spam: 'test'
app.post('/login', passport.authenticate('local', {failureRedirect: '/login'}), users.auth);
app.post('/createQalc', requests.addLoanRequest);

app.get('/api', function (req,res) {
var obj = {
spam: 'test'
};
res.send(obj);
});

//app.get('/myQalcs', index.myQalcs);
app.get('/myQalcs', requests.getCalcs);
app.get('/qalc', requests.getLoanCalc);
app.post('/createQalc', requests.addLoanRequest);

/*app.get('/qalcSelect', frontend.qalcSelect);
app.get('/login', frontend.login);
app.get('/myQalc', frontend.myQalc);
app.get('/offerF', frontend.offerF);
app.get('/qalc', frontend.qalc);
app.get('/users', frontend.users);*/

app.get('/upload', upload.uploadForm);
app.post('/upload', upload.upload);
app.get('/viewfiles', upload.viewFiles);

function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.cookie('lasturl', req.path);
res.redirect('/login');
}
res.send(obj);
});

/*function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next();}
else {res.redirect('/login')};
res.cookie('lasturl', req.path);
res.redirect('/login');
*/

function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.cookie('lasturl', req.path);
res.redirect('/login');
}

}

};