forked from WildCodeSchool/quest-express-post-put
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (30 loc) · 977 Bytes
/
index.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
// dotenv loads parameters (port and database config) from .env
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const connection = require('./db');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// respond to requests on `/api/users`
app.get('/api/users', (req, res) => {
// send an SQL query to get all users
connection.query('SELECT * FROM user', (err, results) => {
if (err) {
// If an error has occurred, then the client is informed of the error
res.status(500).json({
error: err.message,
sql: err.sql,
});
} else {
// If everything went well, we send the result of the SQL query as JSON
res.json(results);
}
});
});
app.listen(process.env.PORT, (err) => {
if (err) {
throw new Error('Something bad happened...');
}
console.log(`Server is listening on ${process.env.PORT}`);
});