-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
44 lines (30 loc) · 993 Bytes
/
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
var express = require('express');
var fs = require('fs');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
var products = [];
app.use("/public", express.static(__dirname + "/public"));
app.engine('html', require('ejs').renderFile);
app.get('/', function(req, res) {
res.render('index.ejs');
});
app.post('/addData', function(req, res){
console.log(req.body);
var newProduct = req.body.newProduct;
var newSales = req.body.newSales;
var newProfit = req.body.newProfit
var dataToWrite = newProduct+','+newSales+','+newProfit+'\n';
products.push(newProduct);
res.redirect('/addData');
fs.appendFile('product_file.txt', dataToWrite, finished);
function finished(){
console.log('added product data');
};
});
app.get('/addData', function(req, res){
res.render('data.html', {products: products});
});
app.listen(3000, function(){
console.log('Server is running on localhost:3000');
});