-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
60 lines (51 loc) · 1.65 KB
/
script.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
import cors from 'cors';
import dotenv from 'dotenv';
import express from 'express'
import mysql from 'mysql2';
dotenv.config();
//app is an instance of express
var app = express();
//Specifying middleware
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Set up connection to database.
var connection = mysql.createConnection(process.env.DATABASE_URL);
// Connect to database.
connection.connect((err) => {
if (err) {
console.error('Error connecting: ' + err.stack);
return;
}
console.log('Connected as ID ' + connection.threadId);
});
//Route Structure:
//app.METHOD(PATH, HANDLER),
//where PATH is a path on the server and HANDLER is the function executed when the route is matched.
app.post('/contacts', (req, res) => {
const { Name, Email } = req.body;
// var Name = req.body.Name;
// var Email = req.body.Email;
connection.query('INSERT INTO contacts (Name, Email) VALUES (?, ?)', [Name, Email], (err, result) => {
if (err) {
console.error('Error when inserting contact: ' + err.message);
res.status(500).send('Error when inserting contact: ' + err.message);
return;
}
res.status(201).json({ message: 'Successfully entered contact!' });
});
});
app.get('/contacts', (req, res) => {
const query = 'SELECT * FROM contacts';
connection.query(query, (err, results) => {
if (err) {
console.error('Error retrieving contacts: ' + err.stack);
res.status(500).send('Error retrieving contacts');
return;
}
res.json(results);
});
});
app.listen(3000, function() {
console.log('Listening on port 3000!');
});