Skip to content

Instantly share code, notes, and snippets.

@luishrd
Created July 26, 2018 18:40
Show Gist options
  • Save luishrd/8c189a2cc08a58b9af0acc0082712eba to your computer and use it in GitHub Desktop.
Save luishrd/8c189a2cc08a58b9af0acc0082712eba to your computer and use it in GitHub Desktop.
// inside /api
const express = require('express');
const hobbitRoutes = require('./hobbits/hobbitRoutes');
// const server = express(); full app
const api = express.Router();
// middleware
// endpoints that begin with /api
api.use('/hobbits', hobbitRoutes); // /api/hobbits
module.exports = api;
// inside /api/hobbits
const express = require('express');
// const server = express(); full app
const router = express.Router();
// middleware
// endpoints that begin with /api/hobbits
router.get('/', (req, res) => {
res.send('the /hobbits endpoint');
});
router.get('/:id', (req, res) => {
res.send('the /hobbits/:id endpoint');
});
module.exports = router;
const express = require('express');
const server = express();
server.get('/', (req, res) => res.send('API up and running!'));
server.use('/api', apiRoutes);
// server.use('/orcs', orcRoutes);
// server.get('/hobbits', (req, res, next) => {
// console.log('checkpoint 1');
// // next(new Error('failed at 1'));
// db.find()
// .then(hobbits => {
// res.status(200).json(hobbits);
// })
// .catch(err => {
// next({ code: 500, message: 'The hobbit was not found' });
// // res.status(500).json({ code: 500, message: 'The hobbit was not found' });
// });
// });
server.get('/users', (req, res, next) => {
console.log('checkpoint 1');
// next(new Error('failed at 1'));
db.find()
.then(users => {
res.status(200).json(users);
})
.catch(err => {
next({ code: 500, message: 'The hobbit was not found' });
// res.status(500).json({ code: 500, message: 'The user was not found' });
});
});
server.use((err, req, res, next) => {
// save the error to the database
// send an email to the responsible party
switch (err.code) {
case 400:
case 11345:
res.status(403).send({
success: false,
data: undefined,
title: err.message,
description: '',
recovery: 'Please require access to this resource',
});
case 404:
default:
res.status(500).send({
success: false,
data: undefined,
error: err.message,
recovery: '',
});
}
});
server.use((req, res, next) => {
res.send("This are not the droids you're looking for");
});
server.listen(9000, () => console.log('API running on port 9000'));

request > uppercaser > POST tags

server.post('/tags', uppercaser, (req, res) => {});

inside the uppercaser middleware // grab data from request body // uppercase the tag information // save it to the database

client side routing vs server side routing

// checkout webpack

http://cs11rocks.com/api/ << serving the api portion of the app

http://cs11rocks.com/ << serving the client application http://cs11rocks.com/ << serving the client application

http://cs11rocks.com/users << not handled by server routing, client routing takes over http://cs11rocks.com/clients << not handled by server routing, client routing takes over

Later inside the client app axios.get('/api/clients').then().catch(); > http://cs11rocks.com/api/clients

// middleware

// routes server.api('/api', apiRouter); // server router

server.use('*', express.static('client/public'))

if (process.env.NODE_ENV === 'production') { // serve up production assets app.use(express.static('client/build'))

const path = require('path') app.get('*', (req, res) => { res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html')) }) }

application build/deployment strategies

// single application that has both client and server // single page application + single api server // microservices === breaks up the api into kinda components

npm init -y added the start script to use nodemon yarn add nodemon --dev yarn add express

req > m1 > m2 > m3 > m4 > em

folder structure (BE CONSISTENT)

  • by type: reducers, components, actions
  • by feature: users, clients, orders
  • hybrid: type > feature || feature > type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment