Added Example Project

This commit is contained in:
Jyotirmoy Bandyopadhayaya 2020-10-20 09:13:05 +05:30
parent 9ddd8b3dd1
commit be6a470274
4 changed files with 47 additions and 2 deletions

View File

@ -0,0 +1,11 @@
const express = require("express");
const bodyParser = require("body-parser");
const postRoutes = require("./routes/post");
const app = express();
app.use(bodyParser.json()); //Parses application/json data
app.use("/posts", postRoutes);
app.listen(8080);

View File

@ -0,0 +1,16 @@
exports.getAllPosts = (req, res, next) => {
res.status(200).json({
title: "My First Post",
like: 10,
});
};
exports.addNewPost = (req, res, next) => {
const title = req.body.title;
const likes = req.body.likes;
res.status(201).json({
status: "success",
message: "Post Added successfully",
data: { id: new Date(), title: title, likes: likes },
});
};

View File

@ -4,11 +4,18 @@
"description": "Backend for LPU HRD Project",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"start": "nodemon app.js"
},
"keywords": [
"mern"
],
"author": "",
"license": "ISC"
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1"
},
"devDependencies": {
"nodemon": "^2.0.5"
}
}

11
backend/routes/post.js Normal file
View File

@ -0,0 +1,11 @@
const express = require("express");
const postController = require("../controllers/post");
const router = express.Router();
// GET /posts/
router.get("/", postController.getAllPosts);
// POST /posts/add
router.post("/add", postController.addNewPost);
module.exports = router;