Created
November 12, 2018 06:37
-
-
Save ashishkumarsinghh/631190082400772eae31be87e51cd907 to your computer and use it in GitHub Desktop.
Handling GET/POST requests in Hapi.js
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
const expModel = require("./dbsetup"); | |
const routes = [ | |
{ | |
method: "GET", | |
path: "/api/", | |
handler: (req, res) => { | |
return `Hello There !`; | |
} | |
}, | |
{ | |
method: "GET", | |
path: "/api/exp", | |
handler: (req, res) => { | |
return expModel.find({}); | |
} | |
}, | |
{ | |
method: "POST", | |
path: "/api/exp", | |
handler: (req, res) => { | |
console.log("received the request."); | |
const error = null; | |
const newExp = new expModel({ | |
title: req.payload.title, | |
description: req.payload.description, | |
time: new Date(), | |
tags: req.payload.tags | |
}); | |
newExp.save((err, ne) => { | |
if (err) { | |
error = err; | |
console.log(err); | |
} else console.log(ne); | |
}); | |
if (!error) { | |
return `Saved.`; | |
} else { | |
return `Sorry.`; | |
} | |
} | |
}, | |
{ | |
method: "GET", | |
path: "/api/exp/{tags}", | |
handler: (req, res) => { | |
return expModel.find({ tags: req.params.tags }); | |
} | |
} | |
]; | |
module.exports = routes; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment