-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (43 loc) · 1.28 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
var express = require('express');
var mysql = require('mysql');
var app = express();
var bodyPasser = require('body-parser');
app.set('view engine','ejs');
app.use(bodyPasser.urlencoded({extended: true}));
app.use(express.static(__dirname + '/public'));
var connection = mysql.createConnection({
host : 'localhost',
user : 'root', // your root username
database : 'join_us', // the name of your db
password : '<Your password here>'
});
// the home page
app.get("/", function(req, res){
var q = 'SELECT COUNT(*) as count FROM users';
connection.query(q, function (error, results) {
if (error) throw error;
var count = results[0].count;
res.render('home', {data: count});
});
});
// adding a /joke route
app.get("/joke", function(req, res){
var joke = "joke joke ......";
res.send(joke);
});
// adding a /random_num route
app.get("/random_num", function(req, res){
var num = Math.floor((Math.random() * 10) + 1);
res.send("Your lucky number is " + num);
});
app.post('/register', function(req,res){
var person = {email: req.body.email};
connection.query('INSERT INTO users SET ?', person, function(err, result) {
console.log(err);
console.log(result);
res.redirect("/");
});
});
app.listen(8888, function () {
console.log('App listening on port ' + '8888');
});