Wednesday, 15 March 2017

Node.js app on Express unable to find specific pug file in views subdirectory

I'm building a simple podcast site with Node.js using Express.js. I'm using Pug as my view engine. Everything works fine until I placed my pug files in subdirectories within the views folder to separate the podcast views from the admin side. I'm fairly new to Node and backend / web development in general so I'd really appreciate any help, as I've scoured the web with no luck. My project structure is as follows:

/podcast
    /bin
        www
    /model
        account.js
        post.js
    /node_modules
    /public
    /routes
        admin.js
        index.js
    /views
        /admin
            index.pug
            layout.pug
            login.pug
            register.pug
        /podcast
            about.pug
            contact.pug
            error.pug
            index.pug
            layout.pug
    app.js
    package.json

My package.json:

{
    ...
    "dependencies" : {
        "body-parser" : "~1.16.0",
        "cookie-parser" : "~1.4.3",
        "debug" : "~2.6.0",
        "express": "~4.14.1",
        ...
        "mongodb" : "^2.2.24",
        "mongoose" : "^4.9.0",
        "morgan" : "~1.7.0",
        "passport" : "^0.3.0",
        "passport-local" : "^1.0.0",
        "passport-local-mongoose" : "^4.0.0",
        "pug" : "~2.0.0-beta10",
        ...
    }
}

My app.js (partial) is as follows:

var express = require('express');
// Boilerplate modules

...

// Import routers
var index = require('./routes/index');
var admin = require('./routes/admin');
...
// view engine setup
app.set('views', path.join(__dirname, './views'));
app.set('view engine', 'pug');

// some middleware
...

app.use('/', index);
app.use('/admin', admin);
...

All of the routes in routes/index.js work perfectly. The issue is with admin.js:

var express = require('express');
var passport = require('passport');
var Account = require('../model/account');
var router = express.Router();

// Handle /admin requests
router.get('/', function(req, res) {
    if(req.user) {
        res.render('admin/index', { user.req });
    } else {
        res.redirect('admin/login');
    }
});

// Handler /admin/login requests
router.get('/login', function(req, res) {
    res.render('admin/login', { user : req.user });
});

router.post('/login', passport.authenticate('local'), function(req, res) { 
    res.redirect('/');
});

router.get('/register', function(req, res) {
    Account.register(new Account({ username: req.body.username }), req.body.password, function(err, account) {
        if(err) {
            return res.render('admin/register', { account : account });
        }
        passport.authenticate('local')(req, res, function() {
            res.redirect('/');
        });
    }
});

Also another note: the app uses MongoDB as the database. Everything works except for when I send a request, either POST or GET to /admin/register.

This is my first Node project, so I may well be missing something completely obvious, but I'd appreciate any help.



via M. Watt

No comments:

Post a Comment