-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
85 lines (62 loc) · 1.7 KB
/
app.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var express = require('express');
var app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
const mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/testapi', function(err) {
console.log(err);
});
app.get('/', function(req, res) {
res.json({msg: 'test API'});
});
const User = require('./user.js');
app.get('/users', async function(req, res) {
try {
//let User = new User();
let users = await User.find();
res.json({status: true, data: users});
} catch (err) {
res.json({status: false, msg : err.message})
}
})
app.post('/users', async function(req, res) {
try {
let user = new User(req.body);
let newUser = await user.save();
res.status(201).json({status : true, data: newUser})
} catch (err) {
res.json({status: false, msg : err.message})
}
});
const Blog = require('./blog');
app.get('/blogs', async function(req, res) {
try {
//let User = new User();
let blogs = await Blog.find().populate('user');
res.json({status: true, data: blogs});
} catch (err) {
res.json({status: false, msg : err.message})
}
})
app.post('/blogs', async function(req, res) {
try {
let blog = new Blog(req.body);
let newBlog = await blog.save();
res.status(201).json({status : true, data: newBlog})
} catch (err) {
res.json({status: false, msg : err.message})
}
});
const Author = require('./author.js');
app.post('/authors', async function(req, res) {
try {
let author = new Author(req.body);
let newAuthor = await author.save();
res.status(201).json({status : true, data: newAuthor})
} catch (err) {
res.json({status: false, msg : err.message})
}
});
app.listen(3000, function() {
console.log('listening port 3000');
});