node.js - Cannot find view in node/express -
as username implies, i'm new node.js. i'm trying learn it. part of process, i'm working setup basic web site. web site show couple of basic web pages , expose single rest endpoint. structure of project is:
config.js home.html start.js routes.js server.js resources css style.css images up.png down.png javascript home.html.js
start.js has main server code. file gets executed via command line using 'node start.js'. once started, server begins listening on port 3000. code in start.js looks this:
var express = require('express'); var app = express(); var userprofilehandler = require('./app/handlers/userprofilehandler'); app.configure(function () { app.engine('html', require('ejs').renderfile); app.set('views', __dirname + '/'); app.use(express.logger({ stream: expresslogfile })); app.use(express.bodyparser()); app.use(express.methodoverride()); app.use(app.router); app.use(express.static(__dirname + '/public')); }); var routes = { userprofiles: new userprofilehandler() }; function start() { routeconfig.setup(app, routes); var port = process.env.port || 3000; app.listen(port); console.log("success: server listening on port %d in %s mode", port, app.settings.env); } exports.start = start; exports.app = app;
my routes.js file has following:
function setup(app, routes) { viewsetup(app); apisetup(app, routes); } function viewsetup(app) { app.get('/', function (req, res) { res.render("/home.html"); }); app.get('/home.html', function (req, res) { res.render("/home.html"); }); } function apisetup(app, routes) { app.get('/api/userprofiles/:username', routes.userprofiles.getuserprofiles); }
i trying load home.html in browser window. attempt visiting http://localhost:3000
, http://localhost:3000/
, http://localhost:3000/home.html
. unfortunately, none of these work. in fact, receive error says:
express 500 error: failed lookup view "/home.html"
i know i'm close. if visit http://localhost:3000/api/userprofiles/me
receive json response i'm expecting. reason, can't seem return html though. home.html file looks following.
<html> <head> <script type='text/javascript' src='/resources/javascript/home.html.js'></script> </head> <body> we're , running! <img src='/resources/images/up.png' /> </body> </html>
its pretty basic html file. if html comes though, i'm concerned javascript file , image references won't accessible. i'm concerned of because i'm not sure how paths , such work in node.
how home.html work in node setup?
thank you!
as view file in same folder main file, below changes should make work
1.change view folder configuration line
from
app.set('views', __dirname + '/');//wont work
to
app.set('views', __dirname);//will work
2.change view render lines
from
res.render("/home.html");//wont work
to
res.render("home.html");//will work
with both changes, view should working fine
update below comments.
the issue mentioned regarding images,css , js due static folder configuration should changed from
app.use(express.static(__dirname + '/public'));
to
app.use(express.static(__dirname + '/resources'));
as static folder named resources
.
but make sure in view refering css/js/image files like
eg:
/css/style.css /images/up.png /images/down.png /javascript/home.html.js
from view file
also if above dint work, check if have given path correctly , can try taking
app.use(express.static(__dirname + '/resources'));
before the
app.use(express.methodoverride()); app.use(app.router);
lines like
app.configure(function () { app.engine('html', require('ejs').renderfile); app.set('views', __dirname + '/'); app.use(express.static(__dirname + '/public')); //try changing position of above line in app.configure , resatrt node app app.use(express.logger({ stream: expresslogfile })); app.use(express.bodyparser()); app.use(express.methodoverride()); app.use(app.router); });
Comments
Post a Comment