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!