Unable to find view in node / express

As follows from my username, I am new to node.js. I'm trying to find out. As part of this process, I am working on setting up a basic website. This website will display a pair of main web pages and set up one REST endpoint. The structure of my project:

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 my main server code. This file is launched through the command line using 'node start.js'. After starting, my server starts listening on port 3000. The code in start.js looks like 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 the 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 am trying to load home.html in a browser window. I am trying to do this by visiting http://localhost:3000 and http://localhost:3000/ and http://localhost:3000/home.html . Unfortunately, none of these works. In fact, I get an error message:

Express 500 error: could not view "/home.html"

I know that I'm near. If I visit http://localhost:3000/api/userProfiles/me , I get a JSON response as I expect. For some reason, I seem to be unable to return the HTML. My home.html file is as follows.

 <html> <head> <script type='text/javascript' src='/resources/javascript/home.html.js'></script> </head> <body> We're up and running! <img src='/resources/images/up.png' /> </body> </html> 

This is a fairly simple HTML file. Even if HTML returns, but I'm interested in a JavaScript file and links to it will not be available. I am concerned about this because I'm not quite sure how the paths and such work are in node.

How do I get home.html to work in my Node setup?

Thanks!

+4
source share
1 answer

since your view file is in the same folder as your main file, the changes below should make it work

1. change the configuration line of the view folder

from

 app.set('views', __dirname + '/');//wont work 

to

 app.set('views', __dirname);//will work 

2. change rendering render lines

from

 res.render("/home.html");//wont work 

to

 res.render("home.html");//will work 

with both changes, the view should work fine

update to comments below.

the problem you mentioned regarding images, css and js is related to the configuration of the static folder that needs to be changed from

 app.use(express.static(__dirname + '/public')); 

to

 app.use(express.static(__dirname + '/resources')); 

since your static folder is called resources .

but make sure you refer to css / js / image files in your view, for example

eg:

 /css/style.css /images/up.png /images/down.png /javascript/home.html.js 

from your view file

In addition, if the above result works, check if you have specified the path correctly, and you can also try by taking

 app.use(express.static(__dirname + '/resources')); 

front

 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 the position of above line in app.configure and resatrt node app app.use(express.logger({ stream: expressLogFile })); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); }); 
+4
source

Source: https://habr.com/ru/post/1498650/


All Articles