rupreckt
rupreckt

Reputation: 31

Sails.js (0.9.8) 404 page to use default layout

Similar to Sails: 404 pages won't use the default layout? but that answer only applies to to 0.10.x which is not an option at this time. I would have asked for clarification there but I don't have enough reputation yet.

My goal is to serve the default layout of my app, with some 404 language and a search bar but every time I try to revamp the 404.ejs page and request myapp.com/routethatdoesntexist, the response I get is 'Unable to get /routethatdoesntexist'

I feel like it's something along the lines of:

var view = 'new404';
res.locals.layout = 'layout';
res.render(view, result, function (err) {
  if (err) {
    return express404Handler();
  }
  res.render(view);
});

But this result is no different than using the provided 404.ejs file. What am I missing?

[EDIT]

Trying the suggestion below, I changed the 404.js file to:

var view = '404test';
res.view('404test', {layout: '404layout_test'});

and 404.ejs to:

Big trouble, little china

404layout_test.ejs is a 1 for 1 copy of our current and functioning layout file. Regardless of which layout I pick, the error message is the same:

{
  "path": {
    "message": "'/Users/brandt/midas/views/404layout_test.ejs'",
    "stack": "Error: '/Users/brandt/midas/views/404layout_test.ejs'\n    at Object.serverErrorOccurred [as 500] (/Users/brandt/midas/config/500.js:28:28)\n    at ServerResponse.respond500 (/Users/brandt/midas/node_modules/sails/lib/hooks/request/index.js:134:21)\n    at /Users/brandt/midas/node_modules/sails/lib/hooks/views/index.js:301:17\n    at Function.app.render (/Users/brandt/midas/node_modules/sails/node_modules/express/lib/application.js:508:5)\n    at ServerResponse.res.render (/Users/brandt/midas/node_modules/sails/node_modules/express/lib/response.js:798:7)\n    at ServerResponse._addResViewMethod.res.view (/Users/brandt/midas/node_modules/sails/lib/hooks/views/index.js:297:15)\n    at Object.pageNotFound [as 404] (/Users/brandt/midas/config/404.js:25:7)\n    at Object.handle (/Users/brandt/midas/node_modules/sails/lib/express/index.js:198:21)\n    at next (/Users/brandt/midas/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:190:15)\n    at Object.favicon [as handle] (/Users/brandt/midas/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/favicon.js:77:7)"
  },
  "INVALID_EXPRESSION_ERR": {
    "message": "51",
    "stack": "Error: 51\n    at Object.serverErrorOccurred [as 500] (/Users/brandt/midas/config/500.js:28:28)\n    at ServerResponse.respond500 (/Users/brandt/midas/node_modules/sails/lib/hooks/request/index.js:134:21)\n    at /Users/brandt/midas/node_modules/sails/lib/hooks/views/index.js:301:17\n    at Function.app.render (/Users/brandt/midas/node_modules/sails/node_modules/express/lib/application.js:508:5)\n    at ServerResponse.res.render (/Users/brandt/midas/node_modules/sails/node_modules/express/lib/response.js:798:7)\n    at ServerResponse._addResViewMethod.res.view (/Users/brandt/midas/node_modules/sails/lib/hooks/views/index.js:297:15)\n    at Object.pageNotFound [as 404] (/Users/brandt/midas/config/404.js:25:7)\n    at Object.handle (/Users/brandt/midas/node_modules/sails/lib/express/index.js:198:21)\n    at next (/Users/brandt/midas/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:190:15)\n    at Object.favicon [as handle] (/Users/brandt/midas/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/favicon.js:77:7)"
  },
  "TYPE_ERR": {
    "message": "52",
    "stack": "Error: 52\n    at Object.serverErrorOccurred [as 500] (/Users/brandt/midas/config/500.js:28:28)\n    at ServerResponse.respond500 (/Users/brandt/midas/node_modules/sails/lib/hooks/request/index.js:134:21)\n    at /Users/brandt/midas/node_modules/sails/lib/hooks/views/index.js:301:17\n    at Function.app.render (/Users/brandt/midas/node_modules/sails/node_modules/express/lib/application.js:508:5)\n    at ServerResponse.res.render (/Users/brandt/midas/node_modules/sails/node_modules/express/lib/response.js:798:7)\n    at ServerResponse._addResViewMethod.res.view (/Users/brandt/midas/node_modules/sails/lib/hooks/views/index.js:297:15)\n    at Object.pageNotFound [as 404] (/Users/brandt/midas/config/404.js:25:7)\n    at Object.handle (/Users/brandt/midas/node_modules/sails/lib/express/index.js:198:21)\n    at next (/Users/brandt/midas/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:190:15)\n    at Object.favicon [as handle] (/Users/brandt/midas/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/favicon.js:77:7)"
  }
}

Setting the res.view to : res.view('404test', {layout: false});

Succeeds, but without the layout.

[EDIT #3]

I've tracked it to the presence of <%- ... %> at the head of the default layout.

Upvotes: 2

Views: 912

Answers (1)

sgress454
sgress454

Reputation: 24958

This should be marked as a duplicate of Sails: 404 pages won't use the default layout?, but in case it lingers in the close vote queue forever:

To use a layout for your custom 404 page in Sails v0.9.x, edit the config/404.js file to use res.view instead of res.render, and provide your layout name in the second argument (or don't, if you want to use the default layout in views/layout.ejs):

res.view("my404page", {layout: "myFancyLayout"})

Upvotes: 1

Related Questions