Skip to content

Instantly share code, notes, and snippets.

@danielkhan
Last active December 31, 2017 00:47
Show Gist options
  • Select an option

  • Save danielkhan/455861917d2e1e634cea0e2dd7265549 to your computer and use it in GitHub Desktop.

Select an option

Save danielkhan/455861917d2e1e634cea0e2dd7265549 to your computer and use it in GitHub Desktop.
const express = require('express');
const router = express.Router();
const isGithubUrl = require('is-github-url');
const ProjectModel = require('../models/Project');
// I am using async because I need await later
router.post('/', async (req, res, next) => {
const url = req.body.githubUrl.trim();
if (!isGithubUrl(url)) {
// Return the error via json
return res.status(400).json({error: 'This is not a valid GitHub URL!'});
}
const project = new ProjectModel();
project.githubUrl = url;
try {
// I'm awaiting the result of my save operation
const saveResult = await project.save();
// Return the created object as result
return res.status(201).json({success: saveResult});
} catch(err) { // Something went wrong
// Pass back the error
return next(err);
}
});
mdoule.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment