이찬

DB connecting

Showing 1 changed file with 52 additions and 8 deletions
1 -const http = require('http');
2 const fs = require('fs'); 1 const fs = require('fs');
3 -const url = require('url');
4 2
5 -var app = http.createServer(function (request, response) { 3 +const express = require('express');
6 - var _url = request.url;
7 4
8 - if (pathname == '/') { 5 +const bodyParser = require('body-parser');
9 6
10 - response.writeHead(200); 7 +const app = express();
11 - response.end(__dirname, _url); 8 +
9 +const port = process.env.PORT || 5000;
10 +
11 +app.use(bodyParser.json());
12 +
13 +app.use(bodyParser.urlencoded({ extended: true }));
14 +
15 +
16 +
17 +const data = fs.readFileSync('./database.json');
18 +
19 +const conf = JSON.parse(data);
20 +
21 +const mysql = require('mysql');
22 +
23 +
24 +
25 +const connection = mysql.createConnection({
26 +
27 + host: conf.host,
28 +
29 + user: conf.user,
30 +
31 + password: conf.password,
32 +
33 + port: conf.port,
34 +
35 + database: conf.database
36 +
37 +});
38 +
39 +connection.connect();
40 +
41 +
42 +
43 +app.get('/api/visiter', (req, res) => {
44 +
45 + connection.query(
46 +
47 + 'SELECT * FROM visiter',
48 +
49 + (err, rows, fields) => {
50 +
51 + res.send(rows);
12 52
13 } 53 }
14 54
55 + )
56 +
15 }); 57 });
16 58
17 -app.listen(3000);
...\ No newline at end of file ...\ No newline at end of file
59 +
60 +
61 +app.listen(port, () => console.log(`Listening on port ${port}`));
......