-
-
Save azamsharp/7c51f6dc6ee49178775b81a3fff73666 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 app = express() | |
const bodyParser = require('body-parser') | |
const mustacheExpress = require('mustache-express') | |
const promise = require('bluebird') | |
app.engine('mustache',mustacheExpress()) | |
app.use(bodyParser.urlencoded({extended :false})) | |
// defined the options for the pg-promise library | |
var options = { | |
promiseLib : promise | |
} | |
// configuring the pg-promise database connection | |
var pgp = require('pg-promise')(options) | |
var connectionString = 'postgres://localhost:5432/nadiasgarden' | |
var db = pgp(connectionString) | |
// mustache pages will be inside the views folder | |
app.set('views','./views') | |
app.set('view engine','mustache') | |
app.get('/newdish',function(req,res){ | |
res.render('newdish') | |
}) | |
app.post('/dishes',function(req,res){ | |
let title = req.body.title | |
let description = req.body.description | |
let price = parseInt(req.body.price) | |
let course = req.body.course | |
let imageURL = req.body.imageURL | |
db.none('INSERT INTO dishes(title,description,price,course,"imageURL") values($1,$2,$3,$4,$5)',[title,description,price,course,imageURL]).then(function(){ | |
db.any('SELECT * FROM dishes').then(function(data){ | |
// res.render(page to render, object to pass to the page) | |
res.render('dishes',{'dishes' : data}) | |
}) | |
}) | |
}) | |
app.get('/dishes',function(req,res){ | |
// fetch dishes from the database | |
db.any('SELECT * FROM dishes').then(function(data){ | |
// res.render(page to render, object to pass to the page) | |
res.render('dishes',{'dishes' : data}) | |
}) | |
}) | |
app.get('/dishes/json',function(req,res){ | |
// fetch dishes from the database | |
db.any('SELECT * FROM dishes').then(function(data){ | |
// res.render(page to render, object to pass to the page) | |
res.status(200).json({ | |
status : 'success', | |
dishes : data | |
}) | |
}) | |
}) | |
app.get('/',function(req,res){ | |
res.send('hello world') | |
}) | |
app.listen(3000, () => console.log('Example app listening on port 3000!')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment