-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
package-lock.json |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const express = require("express"); | ||
|
||
function onError(err,req,res,next){ | ||
return module.exports.onError(err,req,res,next); | ||
} | ||
|
||
module.exports = function(model){ | ||
let router = express.Router(); | ||
router.get("/", async function(req,res,next){ | ||
try{ | ||
res.json(await model.find()); | ||
}catch(err){ | ||
onError(err,req,res,next); | ||
} | ||
}); | ||
|
||
router.get("/:id", async function(req,res,next){ | ||
try{ | ||
res.json(await model.findById(req.params.id)); | ||
}catch(err){ | ||
onError(err,req,res,next); | ||
} | ||
}); | ||
|
||
router.post("/", async function(req,res,next){ | ||
try{ | ||
let item = new model(req.body); | ||
await item.save(); | ||
res.json(item); | ||
}catch(err){ | ||
onError(err,req,res,next); | ||
} | ||
}); | ||
|
||
router.put("/:id", async function(req,res,next){ | ||
try{ | ||
let item = await model.findById(req.params.id); | ||
if (!item){ | ||
throw new Error("Item does not exist", {status: 401}); | ||
} | ||
item.set(req.body); | ||
await item.save(); | ||
res.json(item); | ||
}catch(err){ | ||
onError(err,req,res,next); | ||
} | ||
}); | ||
|
||
router.delete("/:id", async function(req,res,next){ | ||
try{ | ||
let item = await model.findById(req.params.id); | ||
await item.remove(); | ||
res.json(item); | ||
}catch(err){ | ||
onError(err,req,res,next); | ||
} | ||
}); | ||
|
||
return router; | ||
} | ||
|
||
module.exports.onError = function(err, req, res, next){ | ||
console.error(err.message); | ||
console.error(err.stack); | ||
res.status(err.status || 500).json({message:err.message}); | ||
} |
This file contains 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