-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
68 lines (57 loc) · 2 KB
/
server.js
File metadata and controls
68 lines (57 loc) · 2 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
const http = require("http");
//const https = require("https"); // for HTTPS connection
const fs = require("fs");
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require("passport");
const path = require("path");
// create express instance
const app = express();
// DB config
const db = require("./config/keys").mongoURI;
mongoose
.connect(db)
.then(() => {
console.log("Connected to database!");
})
.catch(err => {
console.log("Connection failed:", err);
});
// parse json
app.use(bodyParser.urlencoded({ extended: false })); // false to use qs library to access req.body.something
// when its true then its querystring like ? something in url string
app.use(bodyParser.json());
// APIS
const users = require("./routes/api/users");
const profile = require("./routes/api/profile");
const posts = require("./routes/api/posts");
const port = process.env.PORT || 5000; // for Heroku it use process.env.PORT or we use local 5000
// Passport middleware
// https://www.npmjs.com/package/passport
app.use(passport.initialize());
// Passport Config
require("./config/passport")(passport);
// use routes, here we tell Express to deal each api route separately
app.use("/api/users", users);
app.use("/api/profile", profile);
app.use("/api/posts", posts);
// Server static assets if in production mode
if (process.env.NODE_ENV === "production") {
// set static folder
app.use(express.static("client/build")); // express.static is middleware which servers static files
// if its not any of above route, point it to index.html
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
// const server = https.createServer(
// {
// key: fs.readFileSync("server.key"),
// cert: fs.readFileSync("server.cert"),
// passphrase: "0524"
// },
// app
// );
// const server = http.createServer(app);
app.listen(port, () => console.log(`server now running on ${port}`));