-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
95 lines (80 loc) · 2.28 KB
/
server.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
86
87
88
89
90
91
92
93
94
95
// Include Server Dependencies
const express = require ("express");
const bodyParser = require ("body-parser");
const logger = require ("morgan");
const mongoose = require ("mongoose");
// Require History Schema
const Article = require ("./models/Article");
// Create Instance of Express
const app = express();
// Sets an initial port. We'll use this later in our listener
const PORT = process.env.PORT || 3000;
// Run Morgan for Logging
app.use(logger("dev"));
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 Configuration configuration (Change this URL to your own DB)
if (process.env.MONGODB_URI) {
mongoose.connect(process.env.MONGODB_URI);
} else {
mongoose.connect("mongodb://127.0.0.1:27017")
}
const db = mongoose.connection;
db.on("error", function(err) {
console.log("Mongoose Error: ", err);
});
db.once("open", function() {
console.log("Mongoose connection successful.");
});
//ROUTES
// * `/api/saved` (get) - components will use this to query MongoDB for all saved articles
app.get("/api/saved", function(req, res) {
Article.find({}).limit(5).exec(function(err,doc) {
console.log('hello')
console.log(arguments)
if (err) {
console.log(err)
}
else {
res.send(doc);
}
});
});
// * `/api/saved` (post) - components will use this to save an article to the database
app.post("/api/saved", function(req, res) {
Article.create({
title: req.body.title,
date: Date.now(),
url: req.body.url
}, function(err) {
if (err) {
console.log(err)
}
else {
res.send("SAVED SEARCH")
}
});
});
//* `/api/saved` (delete) - components will use this to delete a saved article in the database
app.delete("/api/saved/:id", function (req, res) {
Article.remove({"_id": req.params.id}, function(err, doc) {
if (err) {
console.log(err)
}
else {
res.send("Deleted")
}
});
});
// Will load single HTML page (with ReactJS) in public/index.html.
app.get("*", function(req, res) {
res.sendFile(__dirname +"/public/index.html")
});
// Listener
app.listen(PORT, function() {
console.log("App listening on PORT: " + PORT);
});