Index Js
Index Js
Index Js
let saltRounds = 3
//functions
async function executeSQL(sql, params) {
return new Promise(function(resolve, reject) {
pool.query(sql, params, function(err, rows, fields) {
if (err) throw err;
resolve(rows);
});
});
}//executeSQL
// lauis Navarro
//! Use of Multer
var storage = multer.diskStorage({
destination: (req, file, callBack) => {
callBack(null, './public/img/') // './public/images/' directory name where save the file
},
filename: (req, file, callBack) => {
callBack(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
}
})
// lauis Navarro
var upload = multer({
storage: storage
});
// routes
app.get('/', async (req, res) => {
let data = await PageModel.getRecentPages(req.path);
res.render('index', { "pages": data })
});
// lauis Navarro
app.post('/admin/create', upload.fields([{ name: 'img', maxCount: 1 }, { name: 'authorImg', maxCount: 1 }]), async (req, res) => {
if (!req.files) {
} else {
let img;
let authorImg;
Object.keys(req.files).forEach((k, i) => {
let file = req.files
if (file['img']) {
img = (file['img'][0].filename)
}
if (file['authorImg']) {
authorImg = file['authorImg'][0].filename
}
})
let sql = "INSERT INTO page_content(title, image_url, body, author_image_url, author_info, page_url, gender) VALUES (?, ?, ?, ?, ?, ?, ? )"
})
// lauis Navarro
app.get('/signup', (req, res) => {
if (req.session.authenticated) {
return res.redirect('/admin')
}
res.render('signup')
})
// lauis Navarro
app.post('/signup', async (req, res) => {
let name = req.body.username;
let password = req.body.pwd;
})
res.render('login')
});
if (matchPassword) {
req.session.authenticated = true;
res.redirect("admin");
} else {
res.render("login", { "error": "Invalid credentials" });
}
});
sql = `SELECT *
FROM main_menu`;
data = await executeSQL(sql);
pageSql = `SELECT page_url, title, pid
FROM page_content`;
pageData = await executeSQL(pageSql);
res.render("menu-edit", { "menu": data, "pages": pageData, "message": "Page Added to Menu!" });
});
sql = `SELECT *
FROM main_menu`;
data = await executeSQL(sql);
let pageSql = `SELECT page_url, title, pid
FROM page_content`;
let pageData = await executeSQL(pageSql);
res.render("menu-edit", { "menu": data, "pages": pageData, "message": "Page Deleted From Menu!" });
});
app.get('/admin/edit-menu-link', menu.renderEditMenuLink);
app.post('/admin/edit-menu-link', menu.editMenuLink, menu.renderEditMenuLink);
app.get('/admin/page-list', page.renderPageList);
app.get('/admin/edit-page', page.renderEditPage);
app.post('/admin/edit-page', upload.fields([{ name: 'image_url', maxCount: 1 }, { name: 'author_image_url', maxCount: 1 }]), page.editPage, page.renderEditPage);
app.get('/:dynamic', page.renderPage);
//start server
app.listen(3000, () => {
console.log("Expresss server running...")
})