-
Notifications
You must be signed in to change notification settings - Fork 0
/
paragraph.js
75 lines (50 loc) · 1.63 KB
/
paragraph.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
var express = require('express')
var app = express()
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/prodesigndb');
var bodyParser = require('body-parser');
var paragraph = require('./model/paragraph.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/paragraph/create', function(req, res) {
var newParagraph = paragraph(req.body);
if (!req.body){
return res.sendStatus(400);
}else{
newParagraph.save(function(err) {
if (err){ res.send(err); };
res.send('Paragraph created!');
});
}
});
app.put('/api/paragraph/update/:id',function(req,res){
paragraph.findOneAndUpdate({ _id: req.params.id }, req.body, function (err, paragraph) {
res.send(req.body);
});
});
app.get('/api/paragraph/:id', function(req, res) {
paragraph.findById(req.params.id, function(err,paragraph){
res.send(paragraph);
});
});
app.get('/api/paragraph/get/url/:post_url', function(req, res) {
paragraph.find({post_url:req.params.post_url}, function(err,paragraph){
res.send(paragraph);
});
});
app.get('/api/paragraph/get/all', function(req, res) {
paragraph.find({},function(err,paragraph){
res.send(paragraph);
});
});
app.listen(8002);
console.log("App listening on port 8002");