Skip to content

Instantly share code, notes, and snippets.

@ashishkumarsinghh
Created November 12, 2018 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashishkumarsinghh/631190082400772eae31be87e51cd907 to your computer and use it in GitHub Desktop.
Save ashishkumarsinghh/631190082400772eae31be87e51cd907 to your computer and use it in GitHub Desktop.
Handling GET/POST requests in Hapi.js
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