Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created May 19, 2020 14:53
Show Gist options
  • Save azamsharp/c17cd38cfaf50fb64097dfb533901555 to your computer and use it in GitHub Desktop.
Save azamsharp/c17cd38cfaf50fb64097dfb533901555 to your computer and use it in GitHub Desktop.
const express = require('express')
const app = express()
const mustacheExpress = require('mustache-express')
// initialize pg promise library
const pgp = require('pg-promise')()
// location of the database
const connectionString = "postgres://localhost:5432/tripsdb"
// create a db object based on the connection string
const db = pgp(connectionString)
// tell express to use mustache templating engine
app.engine('mustache',mustacheExpress())
// the pages are located in views directory
app.set('views','./views')
// extension will be .mustache
app.set('view engine','mustache')
// display all trips
app.get('/trips',(req,res) => {
// access the database
db.any('SELECT trip_id,name from trips;').then(results => {
res.render('index',{trips: results})
})
})
app.listen(3000,() => {
console.log('Server is running...')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment