-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
63 lines (56 loc) · 1.78 KB
/
server.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
var path = require('path')
var fs = require('fs')
var mysql = require('mysql');
var bodyParser = require("body-parser");
var Express = require('express')
var app = new Express()
app.use(bodyParser.json());
var port = 3000
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'q1w2e3r4',
database: 'course'
});
connection.connect();
//connection.end();
app.get('/posts', function (req, res) {
connection.query('SELECT * FROM posts', function(err, rows, fields) {
if (err) throw err;
res.send(JSON.stringify(rows))
});
})
app.post('/posts', function (req, res) {
connection.query('INSERT INTO posts(id,title,content,date) VALUES (null, ?, ?, ?)', [req.body.title, req.body.content, req.body.date], function(err, rows, fields) {
if (err) throw err;
res.send(JSON.stringify(rows))
});
})
app.get('/posts/:id', function (req, res) {
var id = req.params.id;
connection.query('SELECT * FROM posts WHERE id=?', [id], function(err, rows, fields) {
if (err) throw err;
res.send(JSON.stringify(rows[0]))
});
})
app.put('/posts/:id', function (req, res) {
var id = req.params.id;
connection.query('UPDATE posts SET title = ?,content = ?,date = ? WHERE id = ?', [req.body.title, req.body.content, req.body.date, id], function(err, rows, fields) {
if (err) throw err;
res.send(JSON.stringify(rows))
});
})
app.use(function (req, res) {
fs.readFile(path.join(__dirname, '/index.html'), (err, indexData) => {
if (err) throw err
res.set('Content-Type', 'text/html')
res.send(indexData.toString())
})
})
app.listen(port, function (error) {
if (error) {
console.error(error)
} else {
console.info('==> 🌎 Listening on port %s. Open up http://localhost:%s/ in your browser.', port, port)
}
})