forked from glen15/pipeline-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
44 lines (34 loc) · 1.07 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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(bodyParser.json());
const connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME
});
app.get('/', (req, res) => {
res.send('Hello, World@@@@');
});
app.get('/api/text', (req, res) => {
connection.query('SELECT * FROM texts ORDER BY RAND() LIMIT 1', (error, results) => {
if (error) throw Error
res.json({ text: `${results[0].text} by ${results[0].username}` });
});
});
app.post('/api/text', (req, res) => {
const { text, username } = req.body;
const finalText = `${text}`;
connection.query('INSERT INTO texts SET ?', { text: finalText, username }, (error) => {
if (error) throw error;
res.sendStatus(201);
});
});
app.listen(8000, () => {
console.log('Server listening on port 8000');
});