Skip to content

Instantly share code, notes, and snippets.

@wtshek
Last active December 30, 2019 06:09
Show Gist options
  • Save wtshek/802762f252dfe4671ba9092f796bdd43 to your computer and use it in GitHub Desktop.
Save wtshek/802762f252dfe4671ba9092f796bdd43 to your computer and use it in GitHub Desktop.
#express initial setup
# require express
# optional mongoDB and body-parser
const express = require('express');
const mongoose = require("mongoose");
const bodyParesr = require("body-parser");
require("") //schema for mongodb
const app = express();
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODG_URI||"mongodb://localhost:27017/notes");
let db = mongoose.connection;
db.once('open', () => console.log('connected to the database'));
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
app.use(bodyParesr.json());
require("./routes/")(app)
if(process.env.NODE_ENV === "production"){
app.use(express.static("client/build"));
const path = require('path');
app.get("*", (req,res)=>{
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"))
})
}
const PORT = process.env.PORT || 5000;
app.listen(PORT, ()=>{
console.log(`app running on port ${PORT}`);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment