0

Here is my project structure

public/
  css/
  images/
  js/
routes/
  index.js
  users.js
views/
  AdminView/
  layouts/
    layout.hbs
  index.hbs

I am currently having a problem where I cannot load the right view on my Node Express app. Here is some of my code

users.js

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond');
});

router.get('/admin', function(req, res, next) {
  res.render(__dirname + 'AdminView/pages/tables/job-posts', {title: 'Admin     Page'});
});

module.exports = router;

app.js *a portion of it

// view engine setup
app.engine('hbs', hbs({extname: 'hbs', defaultLayout: 'layout' , layoutsDir: __dirname + '/views/layouts'}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

// Render the main view static files
app.use(express.static(path.join(__dirname, 'public')));                 // Render for /
app.use("/career", express.static(__dirname + '/public'));               // Render for /carrer/
app.use("/career/form", express.static(__dirname + '/public'));          // Render for /carrer/form

app.use("/users", express.static(__dirname + "/views/AdminView"));       // Render for /users

// Problematic code below
app.use("/users/admin", express.static(__dirname + "/views/AdminView")); // Render for /users/admin 

app.use('/', routes);
app.use('/users', users);

Here is the problem. When I am trying to access /users/admin, I do not know why it renders the index page on the AdminView folder full with the css, js and images, while it should be rendering the one I specified on the routing (AdminView/pages/tables/job-posts).

Though, if I comment the line that is problematic above, it loads the html file which i specified on the routing file, but does not render the static files.

Side note:

Mauricio Poppe
  • 4,817
  • 1
  • 22
  • 30
Adam
  • 309
  • 1
  • 15
  • you should use the router for the routes, see what [`app.use`](http://stackoverflow.com/questions/7337572/what-does-middleware-and-app-use-actually-mean-in-expressjs) does – Mauricio Poppe May 29 '16 at 04:09
  • @MauricioPoppe Hi, thanks for the fast reply? Can you care to explain by 'use the router for the routes' ? From what I know, I am using the router from my route file – Adam May 29 '16 at 04:27
  • But what about the routes on app.js? they are routes not middlewares – Mauricio Poppe May 29 '16 at 04:32
  • try this res.render(__dirname + 'views/AdminView/pages/tables/job-posts', {title: 'Admin Pag – Dinesh Agrawal May 29 '16 at 10:25
  • @DineshAgrawal Hey man, thank you for your reply. I tried this already out of curiosity. When I use this, It says "Failed to lookup view "C:\Users\adamh\Desktop\NodeProjects\EAFinal\routesviews/AdminView/pages/tables/job-posts" in views directory "C:\Users\adamh\Desktop\NodeProjects\EAFinal\views". – Adam May 29 '16 at 11:15

0 Answers0