Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created May 13, 2020 15:46
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 azamsharp/a64fdca03ace2140ac979163b930a956 to your computer and use it in GitHub Desktop.
Save azamsharp/a64fdca03ace2140ac979163b930a956 to your computer and use it in GitHub Desktop.
const express = require('express')
const app = express()
const tripsRouter = require('./routes/trips')
const usersRouter = require('./routes/users')
console.log(usersRouter)
const mustacheExpress = require('mustache-express')
// setting up Express to use Mustache Express as template pages
app.engine('mustache', mustacheExpress())
// the pages are located in views directory
app.set('views', './views')
// extension will be .mustache
app.set('view engine', 'mustache')
global.trips = [] // global variable which can be used in other routing files
app.use(express.urlencoded()) // for parsing form submitted data
// localhost:3000/trips
// localhost:3000/trips/add-trip
// localhost:3000/trips/delete-trip
// localhost:3000/trips/*
app.use('/trips',tripsRouter)
// /users - Index
// /users/profile - Profile
// /users/view-all - View All Users
app.use('/users',usersRouter)
app.listen(3000, () => {
console.log('Server has started')
})
// /trips/add-trip - adding a trip
// /trips/delete-trip - deleting a trip
// /trips/ - viewing all trips
let express = require('express')
let router = express.Router()
router.post('/add-trip', (req, res) => {
// how to access the city and imageURL form data on the server
let city = req.body.city
let imageURL = req.body.imageURL
let trip = { city: city, imageURL: imageURL }
trips.push(trip)
res.render('add-trip', { city: city })
})
router.post('/delete-trip', (req, res) => {
let city = req.body.city
trips = trips.filter(trip => trip.city != city)
res.redirect('/trips')
})
// /trips
router.get('/', (req, res) => {
//trips = [{ city: 'Houston' }, { city: 'Denver' }]
//let trip = {city: 'Houston', arrival: '02/03/2020', depar}
res.render('trips', { trips: trips })
})
router.get('/add-trip', (req, res) => {
// display add-trip.mustache page
res.render('add-trip')
})
module.exports = router
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment