Last active
April 14, 2020 11:27
-
-
Save kaushalvivek/0771eb3ec65fc76d03aef6d53121ec52 to your computer and use it in GitHub Desktop.
Node server template for MERN stack development
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Imports | |
const express = require('express'); | |
const cors = require('cors'); | |
const mongoose = require('mongoose'); | |
// Get environment | |
require('dotenv').config(); | |
// Create express web app, specify port | |
const app = express(); | |
const port = process.env.PORT || 5000; | |
// Necessary specifications for functioning | |
app.use(cors()); | |
app.use(express.json()); | |
// Connect to DB | |
const uri = process.env.ATLAS_URI; | |
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true } | |
); | |
const connection = mongoose.connection; | |
connection.once('open', () => { | |
console.log("MongoDB database connection established successfully"); | |
}) | |
// Start app | |
app.listen(port, () => { | |
console.log(`Server is running on port ${port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment