Skip to content

Commit

Permalink
add backend boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammadtrad committed Jul 26, 2020
1 parent 95cda94 commit 8560f46
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 8 deletions.
Empty file.
31 changes: 31 additions & 0 deletions build/bundle.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions build/bundle.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "scratch-og",
"name": "bubble-stack",
"version": "1.0.0",
"description": "scratch",
"main": "index.js",
"description": "bubble-stack",
"main": "index.ts",
"scripts": {
"start": "cross-env NODE_ENV=production webpack & cross-env NODE_ENV=production nodemon server/server.js",
"server": "cross-env NODE_ENV=production webpack",
Expand Down
83 changes: 83 additions & 0 deletions server/controllers/stackControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const db = require('../models/stackModels');

const stackController = {};


// stackController.getStack = (req, res, next) => {

// }
// starWarsController.getCharacters = (req, res, next) => {
// // write code here
// // decalre variable that contains SQL query string
// // pass it into db query
// // write and send a query to grab all the columns from the people table.
// const people = 'SELECT p.*, s.name AS species, pl.name AS homeworld FROM people AS p JOIN species AS s ON p.species_id = s._id JOIN planets AS pl ON pl._id = p.homeworld_id';

// db.query(people)
// .then(resolve => {
// res.locals = resolve.rows;
// next();
// })
// .catch((err) => {
// next(err);
// });
// }

// starWarsController.getSpecies = (req, res, next) => {
// // write code here
// const species_id = req.query.id;
// const species = `SELECT species._id, species.classification, species.average_height, species.average_lifespan, species.language, planets.name AS homeworld
// FROM species
// LEFT OUTER JOIN planets
// ON planets._id = species.homeworld_id
// WHERE species._id =${species_id}`;
// db.query(species)
// .then(resolve => {
// res.locals = resolve.rows[0];
// next();
// })
// .catch((err) => {
// next(err);
// });
// }

// starWarsController.getHomeworld = (req, res, next) => {
// // write code here
// const homeworld_id = req.query.id;
// const homeworld = `SELECT _id, rotation_period, orbital_period, diameter, climate, gravity, terrain, surface_water, population
// FROM planets
// WHERE _id = ${homeworld_id}`;
// db.query(homeworld)
// .then(resolve => {
// res.locals = resolve.rows[0];
// next();
// })
// .catch((err) => {
// next(err);
// });
// }

// starWarsController.getFilm = (req, res, next) => {
// // write code here

// next();
// }

// starWarsController.addCharacter = (req, res, next) => {
// // write code here
// console.log(req);
// const characterObj = req.body;
// const character = `INSERT INTO people (name, gender, species_id, birth_year, eye_color, skin_color, hair_color, mass, height, homeworld_id)
// VALUES ('${characterObj.name}', '${characterObj.gender}', '${characterObj.species_id}', '${characterObj.birth_year}', '${characterObj.eye_color}', '${characterObj.skin_color}', '${characterObj.hair_color}', '${characterObj.mass}', '${characterObj.height}', '${characterObj.homeworld_id}')`;
// db.query(character)
// .then(resolve => {
// console.log('this should be inserted');
// res.status(200);
// next();
// })
// .catch((err) => {
// next(err);
// });
//}

module.exports = stackController;
18 changes: 18 additions & 0 deletions server/models/stackModels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { Pool } = require('pg');

const PG_URI = 'postgres://fqimhpba:[email protected]:5432/fqimhpba';

// create a new pool here using the connection string above
const pool = new Pool({
connectionString: PG_URI
});

// We export an object that contains a property called query,
// which is a function that returns the invocation of pool.query() after logging the query
// This will be required in the controllers to be the access point to the database
module.exports = {
query: (text, params, callback) => {
console.log('executed query', text);
return pool.query(text, params, callback);
}
};
11 changes: 11 additions & 0 deletions server/routes/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const express = require('express');
const modelsController = require('../controllers/stackControllers');
const router = express.Router();


router.get('/', (req, res) => {
res.send('Success!');
});

module.exports = router;

11 changes: 7 additions & 4 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
const express = require('express');
const app = express();
const path = require('path');
const PORT = 3000
const PORT = 3000;
const apiRouter = require('./routes/api');

app.use(express.json());

app.use(express.json())
// app.use('/', express.static('client'));
// app.use('/build', express.static(path.join(__dirname, '../build')));
// statically serve everything in the build folder on the route '/build
if (process.env.NODE_ENV === 'production') {
app.use('/', express.static('client'));
app.use('/build', express.static(path.join(__dirname, '../build')));
}

app.use('/api', apiRouter);

// global error handler
app.use('*', (req,res) => res.sendStatus(404) )

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"exclude": [
"node_modules"
]
}
}

0 comments on commit 8560f46

Please sign in to comment.