Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
340CT-TEACHINGMATERIALS/exercises/01_nodejs/02_dynamic_website/bookshop/index.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
81 lines (68 sloc)
2.36 KB
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
#!/usr/bin/env node | |
'use strict' | |
const express = require('express') | |
const handlebars = require('express-handlebars').create({defaultLayout: 'main'}) | |
const bodyParser = require('body-parser') | |
const app = express() | |
app.use(express.static('public')) | |
app.use(bodyParser.urlencoded({ extended: true })) | |
app.engine('handlebars', handlebars.engine) | |
app.set('view engine', 'handlebars') | |
const port = 8080 | |
const sqlite3 = require('sqlite3').verbose() | |
const db = new sqlite3.Database('./bookshop.db', (err) => { | |
if (err) return console.error(err.message) | |
console.log('Connected to the "bookshop.db" SQlite database.') | |
}) | |
/*app.get('/', async(req, res) => { | |
const sql = 'SELECT id, title, description, publisher FROM books;' | |
console.log(sql) | |
db.all(sql, (err, data) => { | |
if(err) console.error(err.message) | |
data.forEach(elem => elem.short = elem.description.slice(0,50)+"...") | |
console.log(JSON.stringify(data, null, 2)) | |
res.render('home', {books: data}) | |
}) | |
})*/ | |
app.get('/', async(req, res) => { | |
let sql = 'SELECT id, title, description, year FROM books;' | |
// -------- | |
let querystring = '' | |
console.log(req.query.q) | |
if(req.query !== undefined && req.query.q !== undefined) { | |
sql = `SELECT id, title, description, year, publisher FROM books | |
WHERE upper(title) LIKE "%${req.query.q}%" | |
OR upper(description) LIKE upper("%${req.query.q}%") | |
OR upper(publisher) LIKE upper("%${req.query.q}%") | |
OR upper(year) LIKE upper("%${req.query.q}%");` | |
querystring = req.query.q | |
} | |
// -------- | |
db.all(sql, (err, data) => { | |
if(err) console.error(err.message) | |
console.log(data) | |
res.render('newindex', {books: data, query: querystring || ' '}) | |
}) | |
}) | |
app.get('/details/:id', (req, res) => { | |
console.log(req.headers.referer) | |
const sql = `SELECT * FROM books WHERE id = ${req.params.id};` | |
console.log(sql) | |
db.get(sql, (err, data) => { | |
if(err) console.error(err.message) | |
data.referer = req.headers.referer; | |
res.render('details', data) | |
}) | |
}) | |
app.get('/form', async(req, res) => res.render('form')) | |
app.post('/add', async(req, res) => { | |
console.log(req.body) | |
const sql = `INSERT INTO books(title, isbn, description) | |
VALUES("${req.body.title}", "${req.body.isbn}", "${req.body.description}");` | |
console.log(sql) | |
db.run(sql, err => { | |
if(err) console.error(err.message) | |
res.redirect('/') | |
}) | |
}) | |
app.listen(port, () => console.log(`app listening on port ${port}`)) |