-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
73 lines (64 loc) · 2.03 KB
/
Copy pathserver.js
File metadata and controls
73 lines (64 loc) · 2.03 KB
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
// Dependencies
var express = require("express");
var bodyParser = require("body-parser");
var logger = require("morgan");
var mongoose = require("mongoose");
var Promise = require("bluebird");
// Model
var News = require("./models/news.js");
var PORT = process.env.PORT || 3000;
var app = express();
process.env.NODE_ENV = 'production';
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.text());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(express.static("public"));
// MongoDB connection
mongoose.Promise = Promise;
if (process.env.NODE_ENV === 'production') {
console.log('node env is', process.env.NODE_ENV);
mongoose.connect('mongodb://heroku_z4c48c2t:q0mhgel9aph8nc77aqh2bgrip2@ds161021.mlab.com:61021/heroku_z4c48c2t');
} else {
console.log('node env is', process.env.NODE_ENV);
mongoose.connect('mongodb://localhost/nyt-search');
}
var db = mongoose.connection;
db.on("error", function (error) { console.log("Mongoose Error: ", error); });
db.once("open", function () { console.log("Mongoose connection successful."); });
app.listen(PORT, function () {
console.log('Running on port: ' + PORT);
});
// Routes for get and post
app.get("/", function (req, res) {
res.sendFile(__dirname + "/public/index.html");
});
app.get("/saved-articles", function (req, res) {
News.find({}).sort({ "title": 1 }).exec(function (error, doc) {
if (error) {
res.send(error);
} else {
res.json(doc);
}
});
});
app.post("/save-article", function (req, res) {
var newArticle = new News(req.body);
newArticle.save(function (error, doc) {
if (error) {
res.send(error);
} else {
res.json(doc._id);
}
});
});
app.delete("/delete-article", function (req, res) {
var deleteID = req.param("id");
News.findByIdAndRemove(deleteID, function (err, data) {
if (err) {
res.send(err);
} else {
res.json(data);
}
});
});