Skip to content

Commit

Permalink
added server code
Browse files Browse the repository at this point in the history
  • Loading branch information
johnphilip283 committed Nov 20, 2018
1 parent 78ddd11 commit a75fcb6
Show file tree
Hide file tree
Showing 7 changed files with 3,316 additions and 10,526 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

package-lock.json
62 changes: 62 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const express = require("express");
const cors = require("cors");
const app = express();
const mysql = require("mysql");

const GET_ALL_PETS_QUERY = "SELECT * FROM pet;";
const ADD_PET_QUERY = "INSERT INTO pet (name, age, description, owner_id, species_id) VALUES (req.body.name, req.body.age, " +
"req.body.description, req.body.owner_id, req.body.species_id)";

const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'petsitting'
});

connection.connect(err => {
if (err) {
return err;
}
});

app.use(cors());

app.get('/', (req, res) => {

});

app.post('/pets/add', function(req, res) {
console.log("here")
connection.query(ADD_PET_QUERY, (err, results) => {
if (err) {
return res.send(err)
} else {
console.log(results);
return res.json({
data: results
})
}
})


});

app.get("/pets", (req, res) => {
console.log("at /pets hurr")
connection.query(GET_ALL_PETS_QUERY, (err, results) => {
if (err) {
return res.send(err)
} else {
return res.json({
data: results
})
}
})
});



app.listen(4000, () => {
console.log("potato on port 4000");
});
Loading

0 comments on commit a75fcb6

Please sign in to comment.