0
votes

app.js:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var newproject = require('./routes/newProject');

var app = express();



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

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

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



// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

index.js

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

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

module.exports = router;

newproject .js

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

router.get('/newproject', function(req, res) {
    res.render('newProject');
});


module.exports = router;

newProject.ejs

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<body>
hello
</body>
</html>

and a similar index.ejs in views .

My home directory works ie localhost:3000/ works but when i type localhost:3000/newproject it poses the following error :

Error: Failed to lookup view "error" in views directory "F:\project\pcgWebApp\views" at EventEmitter.render (F:\project\pcgWebApp\node_modules\express\lib\application.js:580:17) at ServerResponse.render (F:\project\pcgWebApp\node_modules\express\lib\response.js:971:7) at F:\project\pcgWebApp\app.js:47:7 at Layer.handle_error (F:\project\pcgWebApp\node_modules\express\lib\router\layer.js:71:5) at trim_prefix (F:\project\pcgWebApp\node_modules\express\lib\router\index.js:315:13) at F:\project\pcgWebApp\node_modules\express\lib\router\index.js:284:7 at Function.process_params (F:\project\pcgWebApp\node_modules\express\lib\router\index.js:335:12) at next (F:\project\pcgWebApp\node_modules\express\lib\router\index.js:275:10) at F:\project\pcgWebApp\app.js:36:3 at Layer.handle [as handle_request] (F:\project\pcgWebApp\node_modules\express\lib\router\layer.js:95:5)

anyone know whats going on - ?Please help - have tried doing npm install .

It does work if i place

router.get('/newproject', function(req, res) {
    res.render('newProject');
});

in app.js and change router to app.get..

2
But i want it be placed in newproject.js as i have to query a db in newProject.js and reder a lot of variables to the viewSSS
So i created a error.ejs in views but instead of picking up the /newprojects route it now shows contents of error.ejs.SSS
If my solution worked, select it as the accepted answer + consider giving it an upvote :)Jose Lopez Garcia

2 Answers

0
votes

In newProject.js change this:

//...
router.get('/newproject', function(req, res) {
    res.render('newProject');
});

For this:

//...
router.get('/', function(req, res) {
    res.render('newProject');
});

Since newProject.js seems to be used as an external router, as you've written your code your final endpoint would be:

localhost:3000/newproject/newproject

Instead, you want it to be:

localhost:3000/newproject/

That's why you should specify '/' as the endpoint inside of your route: because route paths in Express are associative

0
votes

I believe that your first error is stemming from this section of code:

app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

More specifically, the part that is trying to render the error view. I would ensure that you have a error.ejs file / view in your views folder.

I would also change your /newproject root inside your new project.js file to just / as the root comes from the file name.