Last active
December 31, 2017 00:47
-
-
Save danielkhan/455861917d2e1e634cea0e2dd7265549 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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