Failed to search view in the views directory for a nodejs app

0

I try to create my first Node JS project but I can not show the .ejs page that I created in my views directory. In effect I get:

mike@mike-thinks:~/Desktop/Exercises$ node app.js 
*******************************************************************
**    Paris Dauphine - Emerging technologies
**    Server ready for business at http://:::8080
*******************************************************************
OPEN : Page1 : My name is  
Error: Failed to lookup view "/pages/page1.ejs" in views directory "/home/mike/Desktop/Exercises/views"
    at Function.render (/home/mike/Desktop/Exercises/node_modules/express/lib/application.js:580:17)
    at ServerResponse.render (/home/mike/Desktop/Exercises/node_modules/express/lib/response.js:1008:7)
    at /home/mike/Desktop/Exercises/app.js:45:7
    at Layer.handle [as handle_request] (/home/mike/Desktop/Exercises/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/mike/Desktop/Exercises/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/mike/Desktop/Exercises/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/mike/Desktop/Exercises/node_modules/express/lib/router/layer.js:95:5)
    at /home/mike/Desktop/Exercises/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/mike/Desktop/Exercises/node_modules/express/lib/router/index.js:335:12)
    at next (/home/mike/Desktop/Exercises/node_modules/express/lib/router/index.js:275:10)

With app.js next:

/*
* Emerging technologies
* Paris Dauphine
*
* WARNING :
*   - console.log is used in this example to follow-up progression. Though, It is not a best practice for production apps :-) !
*   - Application here are implemented under the 'happy path' for the sake of clarity. It means that very limited of error traps are made.
*/

/*
express is required along with some iddlewares
- express-session for the Session management
- body-parser in order to parse the bdo of the pages (as json)
*/
var express = require('express')
var bodyParser = require('body-parser')
var session = require("express-session")

//Creates an Express application
var app = express();
//Enable the parser in order to interpret the POST to retrieve data
app.use(bodyParser.urlencoded({ extended: true}));
app.use(bodyParser.json());
app.use(session({resave: true, saveUninitialized: true, secret: 'ThisWillBeOurSecret', cookie: { maxAge: 60000 }}));

var mysession;

//The profile is initialized in the session with a null value if it doesn't already exist

app.use(function(req, res, next){
  mysession = req.session;
  if (typeof(mysession.myprofile) == 'undefined') {
    mysession.myprofile = "";
  }
  if (typeof(mysession.myname) == 'undefined') {
    mysession.myname = "";
  }
  next();
})

////////////////////////////////////////////////////////////////////////////////
//EXPRESS Routes definition
//Express Route : page1
app.get('/page1/', function (req, res) {
  res.render('/pages/page1.ejs',{myname: mysession.myname,myprofile: mysession.myprofile, });
  console.log ('OPEN : Page1 : My name is ', mysession.myname);
});

//Express Route : POST : The profile is updated
app.post('/page1/profile/add/', function(req, res) {
  mysession.myname=req.body.myname;
  mysession.myprofile=req.body.myprofile;
  console.log ('POST Profile updated for : ',mysession.myname);
  console.log ('JSON Content : ',req.body);
  res.redirect('/page1');
})

//Express Route : No route Found : It is a 404
app.use(function (req,res,next){
  res.status(404);
  res.send('404! File not found')
})

// Defining the Server -
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
  console.log('*******************************************************************');
  console.log('**    Paris Dauphine - Emerging technologies');
  console.log('**    Server ready for business at http://%s:%s', host, port);
  console.log('*******************************************************************');

});

The structure of the project is like this

As the screenshot shows, there is page1.ejs in Exercises/views/pages/ . That's why I do not understand the problem

    
asked by ThePassenger 13.04.2018 в 16:17
source

1 answer

1

You should indicate to express what kind of template you are going to use in your case ejs, and then where your templates will be located try to add these two lines

app.set('views', path.join(__dirname, 'pages'));
app.set('view engine', 'ejs');

so you should only use the render like this

res.render('page1',data);
    
answered by 13.04.2018 в 16:36