-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
74 lines (61 loc) · 2.05 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
var express =require('express');
var app = express();
var path =require("path");
var fs = require('fs');
// var popup = require('popups');
var alert =require('alert-node');
app.use(express.static('public'));
app.use(express.urlencoded({ extended: false }));
const ejs =require('ejs');
app.engine('html', ejs.renderFile);
app.set('views',path.join(__dirname, 'views'));
app.set('view engine', 'html');
app.get("/",function(req,res){
res.sendFile(path.join(__dirname,'./views', 'index.html'));
// res.sendFile(path.join('index.html'));
// res.sendFile('index.html', { root: path.join(__dirname, '../views') });
})
app.listen(3000,function(err){
if(err){
console.log("not started");
}else{
console.log("server started")
}
})
app.post('/', function (req, res) {
// console.log(req.body);
var fields = ['Name', 'Email', 'Company/College', 'Phone Number', 'Questions/Remarks'];
var name = req.body.name;
var email = req.body.email;
var compcoll = req.body.comporcoll;
var phnum = req.body.phonenumber;
var msg = req.body.message;
var data = [name, email, compcoll, phnum, msg];
fs.stat('Registered_data.csv', function (err, stat) {
if (err == null) {
console.log('File exists');
//write the actual data and end with newline
data = data + '\r\n';
fs.appendFile('Registered_data.csv', data, function (err) {
if (err) throw err;
console.log('The data was appended to file!');
// alert(" Registration successfull")
res.render('index');
});
}
else {
//write the headers and newline
console.log('Creating new file...');
fields= (fields + '\r\n' + data + '\r\n');
fs.writeFile('Registered_data.csv', fields, function (err) {
if (err) throw err;
console.log('File created and updated.');
res.render('index');
alert(" Registered successfully.");
});
}
})
// popup.alert({
// content: 'Hello!'
// });
});