Showing
1 changed file
with
55 additions
and
11 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 | + |
| 12 | - | 9 | +const port = process.env.PORT || 5000; |
| 13 | - } | 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 | ||
| 14 | 36 | ||
| 15 | }); | 37 | }); |
| 16 | 38 | ||
| 17 | -app.listen(3000); | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 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); | ||
| 52 | + | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + ) | ||
| 56 | + | ||
| 57 | +}); | ||
| 58 | + | ||
| 59 | + | ||
| 60 | + | ||
| 61 | +app.listen(port, () => console.log(`Listening on port ${port}`)); | ... | ... |
-
Please register or login to post a comment