Reputation: 4713
following demo project Here and blog post Here.
He is using jade engine
which I don't want
to use instead using Angularjs templating and routing.
Root folder is
client > js (contain all js files)
> views > partials > html files
> index.html
After changes in his code I am stuck with the below code
I am unable to send proper response.
if I use res.render('index.html', {layout : null});
than get error when refresh page
ERROR :
Error: Cannot find module 'html'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
if I use res.redirect('/')
than refresh the page always send me root (/) defined in app.js.
Needed : I want to send response or not to be on the same page even if I refresh the browser.
{
path: '/*',
httpMethod: 'GET',
middleware: [function(req, res) {
var role = userRoles.public, username = '';
if(req.user) {
role = req.user.role;
username = req.user.username;
}
res.cookie('user', JSON.stringify({
'username': username,
'role': role
}));
//res.render('index.html', {layout : null});
//res.redirect('/');
}],
accessLevel: accessLevels.public
}
Upvotes: 1
Views: 640
Reputation: 24080
If you're not using a backend template language (like jade), then you want to use res.sendfile instead of res.render. Render will look for a template engine matching the extension of the file (such as .jade) and then run the file through it. In this case, it assumes there must be a rendering engine called html, but there is not. SendFile will simply transfer the file as-is with the appropriate headers.
EDIT:
I'm still not 100% sure what you're asking, but I think what you're saying is you want your wildcard route to redirect people to the homepage if they aren't logged in, but if they are, then let other routes take over.
If that is the case you just need to put in a check in your "middleware" function for the /*
route.
Instead of:
function(req, res) {
res.redirect('/');
}
Use some type of conditional logic:
function (req, res, next) {
if (/* !req.cookie.something or whatever */)
res.redirect('/');
else
next(); // continue on to the next applicable (matching) route handler
}
That way you won't always get caught in the redirect loop. Obviously, the above conditional logic could be asynchronous if necessary.
I still believe res.sendfile
is the correct answer to the other portion of your question, just as the github members also suggested.
Upvotes: 2