Skip to content

Commit

Permalink
connecting database
Browse files Browse the repository at this point in the history
  • Loading branch information
moonpatel committed Aug 13, 2022
1 parent 76ef468 commit dcf83f2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
25 changes: 22 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,40 @@
const express = require('express')
const mongoose = require('mongoose')
const path = require('path')
// require models
const Campground = require('./models/campgrounds')
// port number to listen for requests
const port = 3000

// connect to MongoDB database
mongoose.connect('mongodb://localhost:27017/yelpcamp')

// check the status of database connection
const db = mongoose.connection
db.on("error", console.error.bind(console,"connection error"))
db.once("open", () => {
console.log("Database connected")
})

// create an express application object
const app = express()


// set parameters for rendering templates
app.set('view engine','ejs')
app.set('views',path.join(__dirname,'/views'))
app.set('views',path.join(__dirname,'views/'))

// receive get request for the main page
// RECEIVE REQUESTS
// main page
app.get('/',(req,res) => {
res.render('home')
})

// campgrounds
app.get('/campgrounds',(req,res) => {
const camp = new Campground({title:'Backyard'})
res.send(camp)
})

// listen for incoming requests
app.listen(port, () => {
console.log(`LISTENING ON PORT ${port}`)
Expand Down
14 changes: 14 additions & 0 deletions models/campgrounds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// include mongoose
const mongoose = require('mongoose')
const Schema = mongoose.Schema

// schema for campgrounds
const campgroundSchema = new Schema({
title: String,
location: String,
price: String,
description: String
})

// create a model out of campgroundSchema
module.exports = mongoose.model('Campground',campgroundSchema)

0 comments on commit dcf83f2

Please sign in to comment.