Skip to content

Instantly share code, notes, and snippets.

@kaushalvivek
Last active April 14, 2020 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaushalvivek/0771eb3ec65fc76d03aef6d53121ec52 to your computer and use it in GitHub Desktop.
Save kaushalvivek/0771eb3ec65fc76d03aef6d53121ec52 to your computer and use it in GitHub Desktop.
Node server template for MERN stack development
// 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