diff --git a/package.json b/package.json index 5c88200..ba95a2b 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "server": "nodemon server.js", "client": "npm start --prefix client", "client-install": "npm install --prefix client", - "dev": "concurrently \"npm run server\" \"npm run client\"" + "dev": "concurrently \"npm run server\" \"npm run client\"", + "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client" }, "keywords": [ "developers" diff --git a/routes/api/profile.js b/routes/api/profile.js index 329450d..32c20d7 100644 --- a/routes/api/profile.js +++ b/routes/api/profile.js @@ -34,9 +34,9 @@ router.get( .then(profile => { if (!profile) { errors.noprofile = "There is no profile for this user"; - res.status(404).json(errors); + return res.status(404).json(errors); } - res.json(profile); + return res.json(profile); }) .catch(err => res.status(404).json(err)); } @@ -54,7 +54,7 @@ router.get("/all", (req, res) => { errors.noprofile = "No profile exist"; return res.status(404).json(errors); } - res.json(profiles); + return res.json(profiles); }) .catch(err => res.status(404).json({ noprofile: "No profile exist" })); }); @@ -69,9 +69,9 @@ router.get("/handle/:handle", (req, res) => { .then(profile => { if (!profile) { errors.noprofile = "There is no profile for this user"; - res.status(400).json(errors); + return res.status(400).json(errors); } - res.json(profile); + return res.json(profile); }) .catch(err => res.status(404).json(err)); }); @@ -87,7 +87,7 @@ router.get("/user/:user_id", (req, res) => { .then(profile => { if (!profile) { errors.noprofile = "There is no profile for this user"; - res.status(404).json(errors); + return res.status(404).json(errors); } res.json(profile); }) @@ -146,7 +146,7 @@ router.post( Profile.findOne({ handle: profileFields.handle }).then(profile => { if (profile) { errors.handle = "That profile already exists"; - return res.status(400).json(errors); + return res.status(400).json(errors); } new Profile(profileFields).save().then(profile => res.json(profile)); }); diff --git a/routes/api/users.js b/routes/api/users.js index d71c13b..ecb6db8 100644 --- a/routes/api/users.js +++ b/routes/api/users.js @@ -83,7 +83,7 @@ router.post("/login", (req, res) => { } }); }) - .catch(err => console.log(err)); + .catch(err => res.status(404).json(err)); }); router.get( diff --git a/server.js b/server.js index ac8a462..2f4bd17 100644 --- a/server.js +++ b/server.js @@ -3,6 +3,8 @@ const express = require("express"); const mongoose = require("mongoose"); const bodyParser = require("body-parser"); const passport = require("passport"); +const path = require('path') + const app = express(); const port = process.env.PORT || 5000; @@ -35,4 +37,12 @@ app.use("/api/users", users); app.use("/api/profile", profile); app.use("/api/posts", posts); +// Serve static assets in production +if(process.env.NODE_ENV === 'production') { + app.use(express.static("client/build")) + app.get('*', (req, res) => { + res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html' )) + }) +} + app.listen(port, () => console.log(`Server started on port ${port}`));