rogerkeizer (6) [Avatar] Offline
Since there is not yet a preface in the book I created an express app with node.js 0.10.22

The client.htm needs to be in the public folder of the project in order to make the call to http://localhost:9999/client.htm work

The call to http://localhost:1111/client.htm will fail.

But the call to http://localhost:1111/public/client.htm will work (and I get the "XMLHttpRequest cannot load....." error)

Did I set up the project in the wrong way?
monsur.hossain (22) [Avatar] Offline
Re: different path in listing 2.3
Hi Roger. Thanks for your feedback! Both listings 2.1 and 2.3 have a line of code that looks like this:


This tells Express that the static file directory is the directory that the app is running from.

So suppose all your code lives in a directory called “sample”. The “app.js” and “client.html” files should live inside the "sample" directory. When you run “node app.js”, it should be from the “sample” directory. If you have things configured this way, the client.html file should live under http://localhost:9999/client.html (for same-origin requests) and http://localhost:1111/client.html (for cross-origin requests). There is no need to have an extra “public” directory.

It looks like the Connect middleware documentation mentions __dirname + '/public' (source: For the purposes of this book, you can leave off the '/public' part and just use __dirname.

Let me know if that doesn’t make sense, or if you have things configured differently. In the meantime, I’ll look into posting some instructions for setting up Node and ExpressJS.

rogerkeizer (6) [Avatar] Offline
Re: different path in listing 2.3
Hi Monsur,

Thanks for your reply. And yes it made sense.

Express installed this line for me:
serverapp.use(express.static(path.join(__dirname, 'public')));

I changed it to

This solved my path problem with client.html.

monsur.hossain (22) [Avatar] Offline
Re: different path in listing 2.3
Glad to hear that's fixed! Just out of curiosity, what do you mean by "Express installed this line for me"? Is there some sort of default installation that happens? If so, it'd be nice to incorporate that into the book.
rogerkeizer (6) [Avatar] Offline
Re: different path in listing 2.3
What I mean by that is that Express creates a default app.js with this line of code.
To create an app with express I enter in the cmd (on Windows) c: >express testwebsite
(see this screendump:

This is the default app.js in my testwebsite directory

* Module dependencies.

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {

app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));

I also install nodemon to automatically restart the server everytime I change a script file.

Instead of starting the server with node you start it with nodemon.

Hope this helps!