-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress.js
46 lines (37 loc) · 1.09 KB
/
express.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
const express = require('express')
const port = 8999
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const mysql = require('mysql');
app.use(cors());
app.use(bodyParser.json());
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'travel'
})
app.get('/api', (req,res) => res.json('You are the world'))
app.get('/api/locations', (req,res) => {
connection.query('SELECT * FROM newLocation', (err, rows, filds) => {
if(err) throw err
res.json(rows)
})
})
app.delete('/api/dellocations/:id', (req,res) => {
const id = req.params.id
connection.query('DELETE FROM newLocation WHERE id=?', [id], (err, rows, filds) => {
if(err) throw err
console.log('here')
res.json(rows)
})
})
app.post('/api/addlocations', (req,res) => {
console.log(req.body)
connection.query('INSERT INTO newLocation (Name, Lat, Lng) VALUES(?,?,?)', [req.body.name, req.body.location.lat, req.body.location.lng], (err, rows, filds) => {
if(err) throw err
res.json(rows)
})
})
app.listen(port)