Created
November 30, 2020 12:01
-
-
Save aditya-vijaykumar/90c4778f89c0326be3ca5c580d015217 to your computer and use it in GitHub Desktop.
This file contains 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
const express = require('express'); | |
const mongoose = require('mongoose'); | |
const session = require('express-session'); | |
//Express app init | |
const app = express(); | |
// DB Config | |
const db = 'mongodb://localhost:27017/test'; | |
// Connect to MongoDB | |
mongoose | |
.connect( | |
dbonline, | |
{ useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true, useFindAndModify: false } | |
) | |
.then(() => console.log('MongoDB Connected')) | |
.catch(err => console.log(err)); | |
// Express body parser | |
app.use(express.urlencoded({ extended: true })); | |
// Express session Have to change | |
app.use( | |
session({ | |
secret: 'LoxodontaElephasMammuthusPalaeoloxodonPrimelephas', | |
resave: false, | |
saveUninitialized: false | |
}) | |
); | |
// Routes | |
app.use('/', require('./routes/index.js')); | |
app.use('/users', require('./routes/users.js')); | |
const PORT = process.env.PORT || 3000; | |
app.listen(PORT, console.log(`Server started on port ${PORT}`)); | |
//MONGOOSE CONFIG EXAMPLE | |
//The below code should be in a folder called models/<filename>.js | |
const mongoose = require('mongoose'); | |
const AdminSchema = new mongoose.Schema({ | |
name: { | |
type: String, | |
required: true | |
}, | |
email: { | |
type: String, | |
required: true | |
}, | |
password: { | |
type: String, | |
required: true | |
}, | |
role:{ | |
type: String, | |
default: "admin" | |
}, | |
isVerified: { | |
type: Boolean, | |
default: true | |
}, | |
passwordResetToken:{ | |
type: String | |
}, | |
passwordResetExpires: { | |
type:Date | |
}, | |
date: { | |
type: Date, | |
default: Date.now | |
} | |
}, { | |
collection: 'admins' | |
}); | |
const Admin = mongoose.model('Admin', AdminSchema); | |
module.exports = { | |
Admin | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment