Skip to content

Instantly share code, notes, and snippets.

@chrisckchang
Created September 28, 2015 21:07
Show Gist options
  • Save chrisckchang/a3c1c3057f51f9f4e45f to your computer and use it in GitHub Desktop.
Save chrisckchang/a3c1c3057f51f9f4e45f to your computer and use it in GitHub Desktop.
Contact list /contacts POST request
app.post("/contacts", function(req, res) {
var newDoc = req.body;
newDoc.createDate = new Date();
if (req.body.firstName === undefined || req.body.lastName === undefined) {
handleError(null, res, "Must provide a contact name.");
}
if (req.body.email === undefined) {
handleError(null, res, "Must provide an email.");
}
db.collection("contacts").insertOne(newDoc, function(err, doc) {
if (err) {
handleError(err, res, "Failed to create new contact.");
} else {
res.status(201).json(doc.ops[0]);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment