Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ver.7_4
- //https://getbootstrap.com/docs/4.0/components/modal/
- fs = require('fs');
- if (!fs.existsSync('./images')){
- fs.mkdirSync('./images');
- }
- bcrypt = require('bcryptjs');
- sqlite3 = require('sqlite3');
- db = new sqlite3.Database('7_3.sqlitedb');
- db.serialize();
- db.run(`CREATE TABLE IF NOT EXISTS guestbook(
- id INTEGER PRIMARY KEY,
- user TEXT NOT NULL,
- msg TEXT,
- url TEXT,
- date_created TEXT,
- date_modified TEXT)`
- );
- db.parallelize();
- express = require('express');
- bodyParser = require('body-parser');
- cookieParser = require('cookie-parser');
- session = require('express-session');
- pug = require('pug');
- fileUpload = require('express-fileupload');
- app = express();
- app.use(bodyParser.urlencoded({ extended: true }));
- app.set('view engine', 'pug');
- app.set('views','./');
- app.use(cookieParser());
- app.use(session({
- secret: 'random string',
- resave: true,
- saveUninitialized: true,
- }));
- app.use(fileUpload());
- app.use('/images', express.static('./images'));
- app.listen(8080);
- //......>node
- //>require('bcryptjs').hash('demo', 5, (err, hash) => {console.log(hash);});
- //>.exit
- users = {
- ivan: '$2a$05$FIsj86AKAr5JnYg6DujY4.96JBPQCBT/POVh5LJH3hea6I3UvG5D6', //123
- radka: '$2a$05$zcGdceWWgx8zoAeznnRC2.j5HsZH4Z2W1AOIv/aYCWsDIs4QU6iMa', //888
- nasko: '$2a$05$6OBgnb6tpI4GmlrTvaxPB.fR0QhOieMyvN4EDmhSacU4bP0TsqzGW', //777
- demo: '$2a$05$8/jm5CN0Er1iqkyoNlosQu5M7Acq.9wS6yNVhd7e72L7nThMTmQRW' //demo
- };
- app.get('/login', function(req, res) {
- //If template is in file login.pug
- //res.render('login', {info: 'PLEASE LOGIN'});
- res.send(pug.render(login, {info: 'PLEASE LOGIN'}));
- });
- app.post('/login', function(req, res){
- bcrypt.compare(req.body.password, users[req.body.username] || "", function(err, is_match) {
- if(err) throw err;
- if(is_match === true) {
- req.session.username = req.body.username;
- req.session.count = 0;
- res.redirect("/guestbook");
- } else {
- res.send(pug.render(login, {warn: 'TRY AGAIN'}));
- }
- });
- });
- app.get('/logout', (req, res) => {
- req.session.destroy();
- res.redirect("/");
- });
- app.all('*', function(req, res, next) {
- if(req.session.username) return next();
- else res.redirect("/login");
- });
- //CRUD
- //cREADud
- app.get('/guestbook', (req, res) => {
- req.session.count++;
- s = "User: " + req.session.username + " Count: " + req.session.count;
- s += " :: <a href=''>reload</a> :: <a href='logout'>LOGOUT</a> :: ";
- s += new Date();
- let rows = [];
- db.all('SELECT * FROM guestbook ORDER BY date_modified DESC;', function(err, rows) {
- if(err) throw err;
- res.send(pug.render(gb, {'info': s, rows: rows}));
- });
- });
- //CREATErud Add message Picture URL
- app.post('/save',(req, res) => {
- db.run(`
- INSERT INTO guestbook(
- user,
- msg,
- url,
- date_created,
- date_modified
- ) VALUES (
- ?,
- ?,
- ?,
- DATETIME('now','localtime'),
- DATETIME('now','localtime'));
- `,
- [req.session.username, req.body.msg || "", req.body.url],
- (err) => {
- if(err) throw err;
- res.redirect('/guestbook');
- });
- });
- //CREATErud2 Add message2 Picture upload
- app.post('/upload',(req, res) => {
- url = "";
- if(req.files && req.files.file) {
- req.files.file.mv('./images/' + req.files.file.name, (err) => {
- if (err) throw err;
- });
- url = '/images/' + req.files.file.name;
- }
- db.run(`
- INSERT INTO guestbook(
- user,
- msg,
- url,
- date_created,
- date_modified
- ) VALUES (
- ?,
- ?,
- ?,
- DATETIME('now','localtime'),
- DATETIME('now','localtime'));
- `,
- [req.session.username, req.body.msg || "", url],
- (err) => {
- if(err) throw err;
- res.redirect('/guestbook');
- });
- });
- //cruDELETE
- app.post('/delete',(req, res) => {
- db.run('DELETE FROM guestbook WHERE id = ?', req.body.id, (err) => {
- if(err) throw err;
- res.redirect('/guestbook');
- });
- });
- //crUPDATEd POST
- app.post('/update',(req, res) => {
- db.run(`UPDATE guestbook
- SET user = ?,
- msg = ?,
- url = ?,
- date_modified = DATETIME('now','localtime')
- WHERE id = ?;`,
- req.session.username,
- req.body.msg,
- req.body.url,
- req.body.id,
- (err) => {
- if(err) throw err;
- res.redirect('/guestbook');
- });
- });
- app.all('*', function(req, res) {
- res.send("No such page! Go to: <a href='/guestbook'>main page</a>");
- });
- login = `
- html
- head
- link(rel="stylesheet", href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css")
- body
- if info
- h1.alert.alert-info=info
- if warn
- h1.alert.alert-warning #{warn}
- form.mx-auto.border.border-primary.w-25.p-3(method="post")
- div.form-group
- label Username:
- input.form-control(type="text" name="username" placeholder=" Enter user name " value="demo")
- div.form-group
- label Password:
- input.form-control(type="password" name="password" value="demo")
- div.form-group
- input.btn.btn-primary(type="submit" value="Login")
- `;
- gb = `
- html
- head
- link(rel="stylesheet", href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css")
- body
- if info
- h1.alert.alert-info!= info
- div.container
- h2 Guest Book
- button.btn.btn-success(data-toggle="modal" data-target="#modalAdd") + Add New Message (with URL) +
- span
- button.btn.btn-success(data-toggle="modal" data-target="#modalAdd2") + Add New Message2 (Picture upload) +
- table.table.table-striped.table-hover#mytable
- thead.bg-warning.text-uppercase.font-weight-bold
- tr
- td id
- td user
- td msg
- td img
- td date_created
- td ^date_modified^
- td action
- tbody
- each row in rows
- tr
- td= row.id
- td= row.user
- td
- pre= row.msg
- td
- img.img-fluid.rounded-circle.img-thumbnail(src=row.url)
- td= row.date_created
- td= row.date_modified
- td
- div.row
- a.btn.btn-info.edit(href="javascript:void(0);" data-id=row.id data-msg=row.msg data-url=row.url) EDIT
- span
- a.btn.btn-danger.delete(href="javascript:void(0);" data-id=row.id) X
- //modal form ADD - Picture URL
- form(action="/save" method="post")
- div.modal.fade#modalAdd(tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true")
- div.modal-dialog(role="document")
- div.modal-content
- div.modal-header
- h5.modal-title#exampleModalLabel Add New Message
- button.close(type="button" data-dismiss="modal" aria-label="Close")
- span(aria-hidden="true") ×
- div.modal-body
- div.form-group
- textarea.form-control(name="msg" placeholder=" Your message " required)
- div.form-group
- input.form-control(type="text" name="url" placeholder=" Picture URL ")
- div.modal-footer
- button.btn.btn-secondary(type="button" data-dismiss="modal") Cancel
- button.btn.btn-success(type="submit") Save
- //modal form ADD2 - File upload
- form(action="/upload" method="post" enctype="multipart/form-data")
- div.modal.fade#modalAdd2(tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true")
- div.modal-dialog(role="document")
- div.modal-content
- div.modal-header
- h5.modal-title#exampleModalLabel Add New Message2
- button.close(type="button" data-dismiss="modal" aria-label="Close")
- span(aria-hidden="true") ×
- div.modal-body
- div.form-group
- textarea.form-control(name="msg" placeholder=" Your message " required)
- div.form-group
- input.form-control(type="file" name="file")
- div.modal-footer
- button.btn.btn-secondary(type="button" data-dismiss="modal") Cancel
- button.btn.btn-success(type="submit") Save
- //modal form DELETE
- form#add-row-form(action="/delete" method="post")
- div.modal.fade#DeleteModal(tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true")
- div.modal-dialog
- div.modal-content
- div.modal-header
- h5.modal-title#modalLabel Delete Confirmation
- button.close(type="button" data-dismiss="modal" aria-label="Close")
- span(aria-hidden="true") ×
- div.modal-body
- strong Are you sure to delete this message?
- div.modal-footer
- input.form-control.id(type="hidden" name="id" required)
- button.btn.btn-secondary(type="button" data-dismiss="modal") Cancel
- button.btn.btn-danger(type="submit") Delete
- //modal form EDIT/UPDATE
- form(action="/update" method="post")
- div.modal.fade#EditModal(tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true")
- div.modal-dialog(role="document")
- div.modal-content
- div.modal-header
- h5.modal-title#modalLabel Edit Message
- button.close(type="button" data-dismiss="modal" aria-label="Close")
- span(aria-hidden="true") ×
- div.modal-body
- div.form-group
- textarea.form-control.msg(name="msg" placeholder=" Your message " required)
- div.form-group
- input.form-control.url(type="text" name="url" placeholder=" Picture URL ")
- div.modal-footer
- input.form-control.id(type="hidden" name="id" required)
- button.btn.btn-secondary(type="button" data-dismiss="modal") Cancel
- button.btn.btn-info(type="submit") Update
- script(src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js")
- script(src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js")
- script.
- $(document).ready(function(){
- //showing modal form DELETE
- $('#mytable').on('click','.delete',function(){
- $('#DeleteModal').modal('show');
- $('.id').val($(this).data('id'));
- });
- //showing modal form EDIT
- $('#mytable').on('click','.edit',function(){
- $('#EditModal').modal('show');
- $('.id').val($(this).data('id'));
- $('.msg').val($(this).data('msg'));
- $('.url').val($(this).data('url'));
- });
- });
- hr.mx-auto(width="80%")
- `;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement