Skip to content

Instantly share code, notes, and snippets.

@mixj93
Last active July 25, 2021 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mixj93/20acbc21dd609c03958488a4d759f99d to your computer and use it in GitHub Desktop.
Save mixj93/20acbc21dd609c03958488a4d759f99d to your computer and use it in GitHub Desktop.
Express Routers
const express = require('express');
const { getElementById, getIndexById, updateElement,
seedElements, createElement } = require('./utils');
let animals = [];
seedElements(animals, 'animals');
animalsRouter = express.Router();
// Get all animals
animalsRouter.get('/', (req, res, next) => {
res.send(animals);
});
// Get a single animal
animalsRouter.get('/:id', (req, res, next) => {
const animal = getElementById(req.params.id, animals);
if (animal) {
res.send(animal);
} else {
res.status(404).send();
}
});
// Create an animal
animalsRouter.post('/', (req, res, next) => {
const receivedAnimal = createElement('animals', req.query);
if (receivedAnimal) {
animals.push(receivedAnimal);
res.status(201).send(receivedAnimal);
} else {
res.status(400).send();
}
});
// Update an animal
animalsRouter.put('/:id', (req, res, next) => {
const animalIndex = getIndexById(req.params.id, animals);
if (animalIndex !== -1) {
updateElement(req.params.id, req.query, animals);
res.send(animals[animalIndex]);
} else {
res.status(404).send();
}
});
// Delete a single animal
animalsRouter.delete('/:id', (req, res, next) => {
const animalIndex = getIndexById(req.params.id, animals);
if (animalIndex !== -1) {
animals.splice(animalIndex, 1);
res.status(204).send();
} else {
res.status(404).send();
}
});
module.exports = animalsRouter;
const express = require('express');
const app = express();
const PORT = process.env.PORT || 4001;
// Use static server to serve the Express Yourself Website
app.use(express.static('public'));
// Import and mount the expressionsRouter
const expressionsRouter = require('./expressions.js');
app.use('/expressions', expressionsRouter);
const animalsRouter = require('./animals.js');
app.use('/animals', animalsRouter);
app.listen(PORT, () => {
console.log(`Server is listening on ${PORT}`);
});
const express = require('express');
const { getElementById, getIndexById, updateElement,
seedElements, createElement } = require('./utils');
let expressions = [];
seedElements(expressions, 'expressions');
expressionsRouter = express.Router();
// Get all expressions
expressionsRouter.get('/', (req, res, next) => {
res.send(expressions);
});
// Get a single expression
expressionsRouter.get('/:id', (req, res, next) => {
const foundExpression = getElementById(req.params.id, expressions);
if (foundExpression) {
res.send(foundExpression);
} else {
res.status(404).send();
}
});
// Update an expression
expressionsRouter.put('/:id', (req, res, next) => {
const expressionIndex = getIndexById(req.params.id, expressions);
if (expressionIndex !== -1) {
updateElement(req.params.id, req.query, expressions);
res.send(expressions[expressionIndex]);
} else {
res.status(404).send();
}
});
// Create an expression
expressionsRouter.post('/', (req, res, next) => {
const receivedExpression = createElement('expressions', req.query);
if (receivedExpression) {
expressions.push(receivedExpression);
res.status(201).send(receivedExpression);
} else {
res.status(400).send();
}
});
// Delete an expression
expressionsRouter.delete('/:id', (req, res, next) => {
const expressionIndex = getIndexById(req.params.id, expressions);
if (expressionIndex !== -1) {
expressions.splice(expressionIndex, 1);
res.status(204).send();
} else {
res.status(404).send();
}
});
module.exports = expressionsRouter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment