Peter Pik Peter Pik - 18 days ago 6
Node.js Question

Jade + express Failed to lookup view in views directory

this question has been asked a couple of times before, but i can't seem to solve this based on other answers. so whatever i do i seem to keep getting

Failed to lookup view "API" in views directory "/Users/MYUSER/Desktop/matchmaking/app/views"
. even though the path seem right?

file structure

├───app
│ └───bin
│ └───models
│ └───node_modules
│ └───public
│ │ └───images
│ │ └───javascripts
│ │ └───stylesheets
│ └───routes
│ │ └───index
│ │ └───groups
│ │ └───index
│ │ └───users
│ └───views
│ │ └───error.jade
│ │ └───index.jade
│ │ └───layout.jade
│ └───app.js
│ └───package.json
│ └───config.js
│ └───upload.js


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 routes = require('./routes/index');
var users = require('./routes/users');
var games = require('./routes/games');
var groups = require('./routes/groups');

var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
var config = require('./config'); // get our config file

var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(config.database); // connect to our database



var app = express();

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

// 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('/api', routes);
app.use('/api/users', users);
app.use('/api/games', games);
app.use('/api/groups', groups);

// 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 handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});


module.exports = app;


index.js

var express = require('express');
var router = express.Router();
var graph = require('fbgraph');
var User = require('../models/user');
var jwt = require('jsonwebtoken');
var bodyParser = require('body-parser');
var AWS = require('aws-sdk');
var request = require('request');
var uploader = require('../upload');
var config = require('../config');
var mongoose = require('mongoose');



/* GET home page. */
router.get('/', function(req, res, next) {
res.render('API', { title: 'Welcome to the api' });
});

module.exports = router;

Answer

Install jade

npm install jade

make sure there is views/API.jade, if you are rendering this page...please show the controller code

in your controller you are rendering API.JADE res.render("API",..

create a file in views folder .. API.jade

**API.jade**


extends layout
block content
h1= title
p Welcome to #{title}