-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver-AK.js
More file actions
70 lines (48 loc) · 1.8 KB
/
Copy pathserver-AK.js
File metadata and controls
70 lines (48 loc) · 1.8 KB
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
var mysql = require("mysql");
require("dotenv").config();
var fs = require("fs");
var express = require('express');
var app = express();
//you need this to be able to process information sent to a POST route
var bodyParser = require('body-parser');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// parse application/json
app.use(bodyParser.json());
var path = require("path");
// Serve static content for the app from the "public" directory in the application directory.
// you need this line here so you don't have to create a route for every single file in the public folder (css, js, image, etc)
//index.html in the public folder will over ride the root route
app.use(express.static("public"));
app.set('view engine', 'ejs');
// Initializes the connection variable to sync with a MySQL database
var connection = mysql.createConnection({
host: process.env.DB_HOST,
// Your port; if not 3306
port: 3306,
// Your username
user: process.env.DB_USER,
// Your password
password: process.env.DB_PASSWORD, //placeholder for your own mySQL password that you store in your own .env file
database: process.env.DB_NAME //TBD
});
//routes
app.get('/hi', function(req, res) {
res.send("hi");
});
app.get('/flashcards/:category/:id', function(req, res){
// res.render('pages/flashcards');
connection.connect(function(err) {
if (err) {
console.error("error connecting: " + err.stack);
}
connection.query('SELECT * FROM cards WHERE category = ? AND id = ?', [req.params.category, req.params.id], function (error, results, fields) {
if (error) throw error;
// res.json(results);
res.render('pages/flashcards', {
data: results
});
});
});
});
app.listen(3000);