So I'm trying to make a simple page handler for a website (My First NodeJS Project) and struggling to get my head around how to get a MySQL Query to run each time and store the returned data in a variable to be used throughout the application. (I should note I'm using ExpressJS)
Currently in my app.js file I have the following, which seems to be doing it's job at executing for each page load
app.use(pageHandler);
Inside the pageHandler module, I have the following which queries the database based on the URL of the page.
var thisData = app.use(function (req, res, next) {
console.log('Trying to find details for page: ' + url.parse(req.url).pathname);
db.query("SELECT * FROM `pages` WHERE page_url = ?", [url.parse(req.url).pathname], function(err, result) {
res.send(result);
});
});
pageHandler = thisData;
module.exports = pageHandler;
This does infact print, the successful results on the page, but I'm wanting to store them in a variable wihch I can use throughout the application to get for example the title of the page and whatnot. Currently returns the following from database.
[
{
"id":1,
"page_url":"/",
"title":"Home",
"meta_title":"First Node Project",
"meta_description":"Placeholder",
"meta_keywords":"nodejs, keywords, etc",
"parent_id":0,
"position":0,
"template":"home",
"content":"Lipsum..."
}
]
via Josh Scott
No comments:
Post a Comment