-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
143 lines (120 loc) · 4.83 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var express = require('express');
var app = express(); // create our app w/ express
var mongoose = require('mongoose'); // mongoose for mongodb
var morgan = require('morgan'); // log requests to the console (express4)
var bodyParser = require('body-parser'); // pull information from HTML POST (express4)
var methodOverride = require('method-override'); // simulate DELETE and PUT (express4)
var fs = require('fs');
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
var Schema = mongoose.Schema;
var db = mongoose.connection;
var bodyParser = require('body-parser')
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
var Task;
db.on('error', console.error);
db.once('open', function() {
// Create your schemas and models here.
var taskSchema = new Schema({
description: String ,
details: String,
type: String ,
uploadDocPath : String,
remainderTime: String,
finalized: String,
});
Task = mongoose.model('Task',taskSchema);
console.log('Models Created');
});
// configuration =================
mongoose.connect('mongodb://etazero.com:27017/test'); // connect to mongoDB database on modulus.io
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({'extended':'true'})); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(methodOverride());
//routes ========================================
app.get('/api/tasks',function(req,res){
console.log("Task Saved");
Task.find({}, function(err, tasks) {
if (err) {
console.log(err);
}
// object of all the tasks
res.json(tasks);
});
});
app.post('/api/task/:task_id',function(req,res){
Task.findOne({ _id: req.params.task_id }, function (err, doc){
if(err)
{
console.log(err);
res.send(err);
}
console.log(doc);
if(req.param('details', null) != null)
{
doc.details = req.param('details',null);
doc.save();
res.send("Success");
}
});
});
app.post('/api/fileUpload/:task_id', upload.single('document'), function (req, res, next) {
console.log(req.file);
var extn = req.file.originalname.split('.').pop();
var fileName = req.file.filename+"."+extn;
fs.rename(req.file.destination+req.file.filename,req.file.destination+fileName);
Task.findOne({ _id: req.params.task_id }, function (err, doc){
if(err)
{
console.log(err);
res.send(err);
}
console.log(doc);
doc.uploadDocPath = req.file.destination+fileName ;
doc.save();
res.send(doc.uploadDocPath);
});
// req.file is the `document` file
// req.body will hold the text fields, if there were any
});
app.post('/api/tasks',function(req,res){
//Hardcoding for creation of tasks
console.log("Task Creation Started");
console.log(req.param('this', null));
var task = new Task({
description: "Task 2",
type: "Task" ,
uploadDocPath : "/uploads/this/that.png",
remainderTime: "2015:24:06",
finalized: "No",
},function(err,todo){
if(err)
{
res.send(err);
console.log(err);
}
});
console.log("Task Object Created");
Task.find(function(err,todos){
if(err)
res.send(err);
res.json(todos);
});
});
app.get('/:file(*)', function(req, res, next){
var file = req.params.file
, path = __dirname + '/' + file;
res.download(path);
});
app.get('*', function(req, res) {
res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
});
// listen (start app with node server.js) ======================================
app.listen(8080);
console.log("App listening on port 8080");