-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (45 loc) · 1.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const express = require('express');
const { Deta } = require('deta');
require('dotenv').config();
// configure your Deta project
const deta = Deta(process.env.KEY);
// access your DB
const db = deta.Base('Blogs');
const app = express(); // instantiate express
app.use(express.json()) // for parsing application/json bodies
app.get('/',(req,res)=> res.send("Welcome Here !"))
app.post('/newBlog',async(req,res)=>{
let newPost = {
Title : req.body.Title,
Content : req.body.Content
}
let result = await db.put(newPost);
if (result) {
res.json({"message" : "Inserted To DB successFully"});
}else{
res.status(500).json({"message" : "error While sending"});
}
});
app.get('/allBlogs', async(req, res) => {
const blogs = await db.fetch({});
if (blogs) {
res.json(blogs);
} else {
res.status(404).json({"message": "No Blog Post Found!"});
}
});
app.get('/blog/:postKey', async(req,res)=>{
let {postKey} = req.params;
console.log(postKey);
const blog = await db.get(postKey);
if (blog) {
res.json(blog);
} else {
res.status(404).json({"message": "No Blog Post Found!"});
}
})
// app.listen(5500, ()=>{
// console.log("Listning ");
// });
// export 'app'
module.exports = app