Skip to content

Instantly share code, notes, and snippets.

@OmisNomis
Created March 22, 2018 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OmisNomis/80e49be4fb903792e5f69de5ce07c08f to your computer and use it in GitHub Desktop.
Save OmisNomis/80e49be4fb903792e5f69de5ce07c08f to your computer and use it in GitHub Desktop.
Node Express example
'use strict';
const mongoose = require('mongoose');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const app = require('express')();
const port = 8000;
/** Mongoose Schema setup */
const Schema = mongoose.Schema;
const ToDo = mongoose.model(
'ToDo',
new Schema({
title: { type: String },
completed: { type: Boolean },
})
);
/** Mongo Connection */
mongoose
.connect(mlabUri)
.then(() => {
console.log('Connected to mongo');
})
.catch(error => {
console.error(`Initial Mongo connection error: ${error}`);
});
/** App setup */
app.use(morgan('combined'));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
/** Routes */
app.get('/getOne', (req, res, next) => {
ToDo.findOne(req.query, (err, doc) => {
if (err) {
next(err);
}
if (!doc || doc.length < 1) {
console.warn('No document found');
return res.sendStatus(404);
}
res.send(doc).status(200);
});
});
app.get('/getMultiple', (req, res, next) => {
ToDo.find(req.query, (err, docs) => {
if (err) {
return next(err);
}
if (!docs || docs.length < 1) {
console.warn('No documents found');
return res.sendStatus(404);
}
res.send(docs).status(200);
});
});
app.post('/addOne', (req, res) => {
// make sure title is passed to the POST request.
if (!req.body.title || typeof req.body.title !== 'string') {
return res.status(400).send('Title not passed or not a string');
}
const title = req.body.title.toLowerCase();
const completed = req.body.completed ? req.body.completed.toLowerCase() : undefined;
const todo = new ToDo({
title: title,
completed: completed && (completed === 'true' || completed === 'false') ? completed : false,
});
todo.save((err, todo) => {
if (err) {
return next(err);
}
res.send(`ToDo created: ${JSON.stringify(todo)}`).status(201);
});
});
app.put('/updateOne', (req, res, next) => {
ToDo.findOneAndUpdate(req.query, req.body, (err, doc) => {
if (err) {
return next(err);
}
if (!doc || doc.length < 1) {
console.warn('No document found');
return res.sendStatus(404);
}
res.sendStatus(200);
});
});
app.put('/updateMultiple', (req, res, next) => {
ToDo.find(req.query, (err, docs) => {
if (err) return next(err);
if (!docs || docs.length < 1) {
console.warn('No document found');
return res.sendStatus(404);
}
ToDo.updateMany(req.query, req.body, (err, doc) => {
if (err) return next(err);
res.sendStatus(200);
});
});
});
app.delete('/deleteOne', (req, res, next) => {
ToDo.findOneAndRemove(req.query, (err, doc) => {
if (err) return next(err);
if (!doc || doc.length < 1) {
console.warn('No document found');
return res.sendStatus(404);
}
res.send(doc).status(200);
});
});
app.delete('/deleteMultiple', (req, res, next) => {
ToDo.find(req.query, (err, docs) => {
if (err) return next(err);
if (!docs || docs.length < 1) {
console.warn('No document found');
return res.sendStatus(404);
}
ToDo.deleteMany(req.query, (err, doc) => {
if (err) return next(err);
res.sendStatus(200);
});
});
});
/** Error Handler */
app.use((err, req, res, next) => {
return res.status(500).send(err.stack || err);
});
/** Start app */
app.listen(port, () => {
console.info(`App listening on port ${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment