-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthor.js
63 lines (42 loc) · 1.3 KB
/
author.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
var express = require('express')
var app = express()
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/prodesigndb');
var bodyParser = require('body-parser');
var author = require('./model/author.model');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.raw());
app.use(bodyParser.json());
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
res.setHeader('Content-Type', 'application/json');
next();
});
app.set('json spaces', 2);
app.post('/api/author/create', function(req, res) {
var newAuthor = author(req.body);
if (!req.body){
return res.sendStatus(400);
}else{
newAuthor.save(function(err) {
if (err){ res.send(err); };
res.send('Author created!');
});
}
});
app.get('/api/author/:id', function(req, res) {
author.findById(req.params.id, function(err,author){
console.log(req.params.id);
res.send(author);
});
});
app.get('/api/author/get/all', function(req, res) {
author.find({},function(err,author){
console.log('p',author);
res.send(author);
});
});
app.listen(8004);
console.log("App listening on port 8004");