Created
February 7, 2018 08:27
-
-
Save anonymous/90e14b3f809ebfce8ee483f1f94e8cfa 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 exphbs = require('express-handlebars'); | |
const mongoose = require('mongoose'); | |
const bodyParser = require('body-parser'); | |
const app = express(); | |
//Mongo connection | |
mongoose.connect('mongodb://localhost/webapp',(error)=>{ | |
if(error) throw error; | |
console.log('Mongodb Connected'); | |
}); | |
//Load Schema | |
require('./models/Idea'); | |
const Idea = mongoose.model('ideas'); | |
//BodyParser Middleware | |
app.use(bodyParser.urlencoded({ extended: false })) | |
app.use(bodyParser.json()) | |
//Handlebars Middleware | |
app.engine('handlebars', exphbs({ defaultLayout: 'main' })); | |
app.set('view engine', 'handlebars'); | |
app.get('/', (req,res)=>{ | |
const title = 'welcome'; | |
res.render('index',{ | |
title :title | |
}); | |
}); | |
app.get('/about',(req,res)=>{ | |
res.render('about'); | |
}); | |
//Add Ideas | |
app.get('/ideas/add', (req, res) => { | |
res.render('ideas/add'); | |
}); | |
//Process Form | |
app.post('/ideas',(req,res)=>{ | |
let errors = []; | |
if(!req.body.title){ | |
errors.push({text:'Please add a title'}); | |
} | |
if(!req.body.details){ | |
errors.push({text:'Please add some Details'}); | |
} | |
if(errors.length > 0){ | |
res.render('ideas/add',{ | |
errors : errors, | |
title: req.body.title, | |
details: req.body.details | |
}); | |
}else{ | |
res.send('Successful'); | |
} | |
}); | |
const port = 5000; | |
app.listen(port,() => { | |
console.log(`Server is running on port ${port}`); | |
}); |
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 mongoose = require('mongoose'); | |
const Schema = mongoose.Schema; | |
//Create Schema | |
const IdeaSchema = new Schema({ | |
title:{ | |
type: String, | |
required: true | |
}, | |
details:{ | |
type: String, | |
required: true | |
}, | |
date:{ | |
type:Date, | |
default: Date.now | |
} | |
}); | |
mongoose.model('ideas',IdeaSchema); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
here is why I suggested the changes to your module:
after making the suggested change to idea.js:
you can see, by setting the default export of the module to the model, your require should provide you with a model to work with.