This repository has been archived by the owner on May 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
83 lines (68 loc) · 2.25 KB
/
app.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
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
var multer = require("multer");
var fs = require("fs");
var storage = multer.diskStorage({
destination: function (req, file, cb) {
var dir = 'uploads/' + req.body.projectName + Date.now() ;
fs.mkdir(dir)
cb(null, dir);
},
filename: function (req, file, cb) {
cb(null, file.originalname);
}
});
var upload = multer({ storage: storage });
var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/package_master");
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
// Database schema
var orderSchema = new mongoose.Schema({
dateCreated: {type: Date, default: Date.now()},
projectName: String,
subjects: Number,
status: String
});
var Order = mongoose.model("Order", orderSchema);
// Sample data
// var orders =[
// {created : "10/15/2016", name : "Davie Elementary - Underclass 2016", subjects : "856", status : "Pending"},
// {created : "09/15/2016", name : "Cardinal Gibbons - Underclass 2016", subjects : "918", status : "Ready"},
// {created : "10/15/2016", name : "Boca Raton Christian School - Underclass 2016", subjects : "650", status : "Ready"}
// ];
// create item in database
// var newOrder = {projectName : "Davie Elementary - Underclass 2016", subjects : "856", status : "Pending"};
// Order.create(newOrder, function(err, newlyCreated){
// if(err){
// console.log(err);
// }
// });
// Routes
app.get("/orders", function(req, res){
Order.find({}, function(err, allOrders){
if (err) {
console.log("Error: " + err);
} else {
res.render("index", {orders: allOrders});
}
});
});
app.post("/orders", upload.array("studentData", 1), function (req, res){
var projectName = req.body.projectName;
var newOrder = {projectName : projectName, status : "Pending"};
Order.create(newOrder, function(err, newlyCreated){
if(err){
console.log(err);
} else {
res.redirect("/orders");
}
});
});
app.get("/orders/new", function(req, res) {
res.render("new");
});
app.listen(process.env.PORT, process.env.IP, function(){
console.log("Package Master Server has started");
});