Skip to content

Commit

Permalink
creating first api
Browse files Browse the repository at this point in the history
  • Loading branch information
vasu962 committed Sep 7, 2023
0 parents commit 6baccdf
Show file tree
Hide file tree
Showing 854 changed files with 134,217 additions and 0 deletions.
96 changes: 96 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import express from "express";
import bodyParser from "body-parser";

const app = express();
const port = 4000;

// In-memory data store
let posts = [
{
id: 1,
title: "The Rise of Decentralized Finance",
content:
"Decentralized Finance (DeFi) is an emerging and rapidly evolving field in the blockchain industry. It refers to the shift from traditional, centralized financial systems to peer-to-peer finance enabled by decentralized technologies built on Ethereum and other blockchains. With the promise of reduced dependency on the traditional banking sector, DeFi platforms offer a wide range of services, from lending and borrowing to insurance and trading.",
author: "Alex Thompson",
date: "2023-08-01T10:00:00Z",
},
{
id: 2,
title: "The Impact of Artificial Intelligence on Modern Businesses",
content:
"Artificial Intelligence (AI) is no longer a concept of the future. It's very much a part of our present, reshaping industries and enhancing the capabilities of existing systems. From automating routine tasks to offering intelligent insights, AI is proving to be a boon for businesses. With advancements in machine learning and deep learning, businesses can now address previously insurmountable problems and tap into new opportunities.",
author: "Mia Williams",
date: "2023-08-05T14:30:00Z",
},
{
id: 3,
title: "Sustainable Living: Tips for an Eco-Friendly Lifestyle",
content:
"Sustainability is more than just a buzzword; it's a way of life. As the effects of climate change become more pronounced, there's a growing realization about the need to live sustainably. From reducing waste and conserving energy to supporting eco-friendly products, there are numerous ways we can make our daily lives more environmentally friendly. This post will explore practical tips and habits that can make a significant difference.",
author: "Samuel Green",
date: "2023-08-10T09:15:00Z",
},
];

let lastId = 3;

// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));


app.get("/posts", (req, res) => {
console.log(posts);
res.json(posts);
});

app.get("/posts/:id", (req, res) =>{
const id = parseInt(req.params.id);
const post = posts.find((p) => p.id === id);
if(post){
return res.json(post);
}else{
res.sendStatus(404).json({message: "post not found"});
}
});

app.post("/posts", (req, res) => {
const newId = lastId += 1;
const post ={
id : newId,
title: req.body.title,
content: req.body.content,
author: req.body.author,
date: new Date(),
}
lastId = newId;
posts.push(post);
res.status(201).json(post);
});

app.patch("/posts/:id", (req, res) => {
const id = parseInt(req.params.id);
const post = posts.find((p) => p.id === id);
if(!post) return res.status(404).json({message: "Post not found!"});

if(req.body.title) post.title = req.body.title;
if(req.body.content) post.content = req.body.content;
if(req.body.author) post.author = req.body.author;

res.json(post);
});

app.delete("/posts/:id", (req, res) => {
const id = parseInt(req.params.id);
const index = posts.findIndex((p) => p.id === id);
if(index > -1){
posts.splice(index, 1);
res.json({message : "Post deleted"});
}else{
res.status(404).json({message: "Post not found!"});
}
});

app.listen(port, () => {
console.log(`API is running at http://localhost:${port}`);
});
12 changes: 12 additions & 0 deletions node_modules/.bin/ejs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions node_modules/.bin/ejs.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions node_modules/.bin/ejs.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions node_modules/.bin/jake

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions node_modules/.bin/jake.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions node_modules/.bin/jake.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions node_modules/.bin/mime.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions node_modules/.bin/mime.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6baccdf

Please sign in to comment.