-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (48 loc) · 1.35 KB
/
index.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
var express = require("express");
var path = require("path");
var logger = require("morgan");
var bodyParser = require("body-parser");
var passport = require("passport");
require("./config/passport.js")(passport);
var post = require("./routes/post");
var auth = require("./routes/auth");
var app = express();
var mongoose = require("mongoose");
var cors = require("cors");
// custom localhost port
var PORT = 3001;
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: "false" }));
// CORS
app.use(cors());
// connect data
//mongoose.Promise = require("bluebird");
mongoose
.connect("mongodb://localhost:27017/mern-crud", {
useNewUrlParser: true,
promiseLibrary: require("bluebird")
})
.then(() => console.log("Successfully Connect!"))
.catch(err => console.error(err));
app.use("/api/post", post);
app.use("/api/auth", auth);
app.get("/", (req, res) => {
return res.end("Successfully Working API Server!");
});
// 404 error
app.use(function(req, res, next) {
var err = new Error("404 Not Found");
err.status = 404;
next(err);
});
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "client/build")));
//
app.get("*", (req, res) => {
res.sendfile(path.join((__dirname = "client/build/index.html")));
});
}
// start port
app.listen(PORT);
module.exports = app;