-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (58 loc) · 1.56 KB
/
index.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
const express = require('express');
const path = require('path');
const port = 8000;
const db = require('./config/mongoose');
const Expense = require('./models/expense');
const app = express();
app.set('view engine' , 'ejs');
app.set('views' , path.join(__dirname , 'views'));
app.use(express.urlencoded());
app.use(express.static('assets'));
var priceManager = [
{
product_name:"toothpaste",
price : "120"
},
{
product_name:"file",
price:"35"
}
];
app.get('/' , function (req , res) {
// res.send('server chl riya hai ');
return res.render('home' ,{
title : "expense manager",
priceManager:priceManager
});
});
app. post('/add-item' , function(req , res){
// try{
// }
Expense.create({
product_name:req.body.product_name,
price:req.body.price
} , function (err , newExpense) {
if(err){
console.log("error creating an expense");
return;
}
console.log("***" , newExpense);
return res.redirect('back');
});
// priceManager.push(req.body);
// res.redirect('back');
});
app.get('/delete-item' , function(req , res){
let productName = req.query.product_name;
let productIndex = priceManager.findIndex(product => product.product_name == productName);
if(productIndex!=-1){
priceManager.splice(productIndex , 1);
}
return res.redirect('back');
})
app.listen(port, function (err) {
if(err){
console.log("error in running the server" , err);
}
console.log("server is running good ");
})