I am at my wits end with this one. Using a template from express-generator. My code is as follows:
"use strict"
 var express = require('express');
 var router = express.Router();
 var sqlite = require ("sqlite3");
 var db = new sqlite.Database("./database/data.db");
 var standard = {...}//JSON
 //Check if path is valid i.e. stored in db 
 router.get('*', function(req, res, next) {
 console.log(req.path);
 db.serialize(function () {
    console.log('test 1');
    var stmt = db.prepare(  'SELECT COUNT (*) as p FROM access WHERE path = ?');
    stmt.get(req.path,function(err, row){
        if(row.p!=1){
            var err = new Error('Not Found');
            err.status = 404;
            console.log('bad things are happening');
            next(err);
        }else {
            next();
        }
    });
});
});
//Render relevant view if path was valid
router.get('*', function(req, res, next) {
console.log(req.path);
db.serialize(function () {
    console.log('test 2');
    var stmt = db.prepare(  'SELECT css.path as cp,renderView as vp FROM access '+
                            'JOIN css_access ON css_access.accessid = access.id '+
                            'JOIN css ON css_access.cssid = css.id '+
                            'WHERE access.path = ?');
    stmt.get(req.path,function(err, row){
        console.log(row.cp +': SQL result :)');
        standard.resources.css=row.cp;
        console.log((standard.resources.css));
        standard.title = 'Project Welcome Page';
        console.log('Testing row.vp: '+row.vp);
        res.render(row.vp,standard);
    });
});
});
Assuming that the the valid path and it's corresponding view is in the database; This works for index.ejs but not for something else like parallax.ejs (in which case i get a 500 error) . I am really confused by this as both of them are in the same folder etc.
Could someone help me out with this please ? :)
via Timsi
 
No comments:
Post a Comment