-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (49 loc) · 1.75 KB
/
server.js
File metadata and controls
65 lines (49 loc) · 1.75 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
const express = require("express");
const path = require("path");
const app = express();
const PORT = process.env.PORT || 3001;
const mongoose = require ("mongoose");
const Book = require("./models/Books");
var MONGODB_URI = "mongodb://heroku_1b5mbwhd:4tadas89mmunku92h6ok4icueo@ds017553.mlab.com:17553/heroku_1b5mbwhd";
// process.env.MONGODB_URI
// "mongodb://heroku_40lj7s5b:f9sd7msbe4vvh0dbbhk78rpprp@ds029466.mlab.com:29466/heroku_40lj7s5b"// "mongodb://localhost:27017/googlebooks"; //process.env.MONGODB_URI ||
//mongoose.connect()
// Define middleware here
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
// Serve up static assets (usually on heroku)
mongoose.connect(MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true });
// Define API routes here
app.get("/api/books", function(request, response) {
Book.find({})
.then(books => {
return response.json(books);
})
.catch((err) => response.status(404).json(err))
})
app.post('/api/books', function(request, response) {
Book.create(request.body)
.then(books => {
response.json(books);
})
.catch(err=> {
response.status(404).json(err);
});
});
app.delete("/api/books/:id", function(request, response) {
console.log("at dele:", request.params.id)
Book.remove({_id: request.params.id})
.then(book => response.json(book))
.catch(err => response.status(404).json(err))
})
// Send every other request to the React app
// Define any API routes before this runs
app.get("*", (request, response) => {
response.sendFile(path.join(__dirname, "./client/build/index.html"));
});
app.listen(PORT, () => {
console.log(`🌎 ==> API server now on port ${PORT}!`);
});