-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
49 lines (39 loc) · 1.45 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
const express = require('express');
const fileUpload = require('express-fileupload');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const path = require('path');
const app = express();
const port = 8000;
app.set('port', process.env.port || port);
const db = mysql.createConnection ({
host: 'localhost',
user: 'root',
password: '',
database: 'schools'
});
// connect to database
db.connect((err) => {
if (err) {
throw err;
}
console.log('Connected to database');
});
global.db = db;
app.set('views', __dirname + '/views'); // perintah untuk express merender views yang ada dalam folder news
app.set('view engine', 'ejs'); // konfigurasi template engine
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // parse form data client
app.use(express.static(path.join(__dirname, 'public'))); // configure express untuk memakai public folder
app.use(fileUpload()); // configure fileupload
const {getStudentIndex} = require('./routes/index');
const {addStudentPage, addStudent, editStudentPage, editStudent, deleteStudent} = require('./routes/students');
app.get('/', getStudentIndex);
app.get('/student/add', addStudentPage);
app.post('/student/add', addStudent);
app.get('/student/edit/:id', editStudentPage);
app.post('/student/edit/:id', editStudent);
app.get('/student/delete/:id', deleteStudent);
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});