I am following the peepcode (part 1) video tutorial on node.js, and I am getting stuck on the first few steps. When I try to go to http://localhost:3000/login, I get the following error:
500 Error: Failed to lookup view "/Users/dantang/Desktop/Programming/Node/basic server/hotpie/apps/authentication/views/login"
I'm guessing it is due to a typo, but I've tried changing the code several times and I can't figure out what is wrong. Would really appreciate it if someone could tell me what should I do? Thank you!
The github repo is https://github.com/tangbj/nodejs
Server.js
/**
* Module dependencies.
*/
require("coffee-script");
var express = require('express')
, http = require('http')
, path = require('path');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
//Routes
require('./apps/authentication/routes')(app);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
login.jade
form(action='/sessions', method='post')
label
| Username
input(type='text', name='user')
label
| Password
input(type='password', name='password')
input(type='submit', name='Submit')
routes.coffee
routes = (app) ->
app.get '/login', (req, res) ->
res.render "#{__dirname}/views/login",
title: 'Login'
stylesheet: 'login'
module.exports = routes
package.json
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.1.0",
"jade": "*",
"coffee-script": "~1.6.1"
}
}
There was something wrong with your login.jade file. Maybe some encoding problem. I created a newLogin.jade file, changed the render function and everything works fine. See the pull request at github.
It will be unable to find the path if you are using Windows which requires backslash instead of forward-slash in linux. So use path.join or path.normalize to get correct paths.
Try to change this in your routes.coffee
res.render "#{__dirname}/views/login",
to
res.render path.join(__dirname, "/views/login"),
and
app.set('views', __dirname + '/views');
to
app.set('views', path.join(__dirname + '../views'));
Otherwise verify the location of the directories/files.