azat-co / expressworks

Learn Express.js from the author of one of the best books on Express.js—Pro Express.js— with this workshop that will teach you basics of Express.js.
MIT License
709 stars 220 forks source link

Exercise 1 fails to verify #43

Closed amahfouz closed 8 years ago

amahfouz commented 9 years ago
  1. Running node v0.10.30 on Windows 7
  2. Installed express.
  3. Installed expressworks.
  4. Wrote the simple program
var express = require('express')
var app = express()

app.get('/', function(req, res) {
  res.write('Hello World!')
  res.end()
})
app.listen(process.argv[2])

And when I run: expressworks verify myprog.js

it fails with the following message

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: connect ECONNREFUSED
    at errnoException (net.js:904:11)
    at Object.afterConnect [as oncomplete] (net.js:895:19)

*. Ran the same program with a hardcoded 3000 port and invoking localhost:3000 worked from the browser and showed the "Hello World!".

rodrigo-medeiros commented 9 years ago

@amahfouz The validation script makes a get request to the /home path, not /:

Create an Express.js app that runs on localhost:3000, and outputs "Hello World!" when somebody goes to '/home'.

Make the change and see if that works.

amahfouz commented 9 years ago

Thanks!

I made the change (which was an obvious mistake on my part):

var express = require('express')
var app = express()

app.get('/home', function(req, res) {
  res.write('Hello World!')
  res.end()
})

app.listen(process.argv[2])

but I still get the same error. The browser still does show the right thing when I hardcode the port and go to http://localhost:3000/home

amahfouz commented 9 years ago

I dug further and found the solution.js for the problem which looked like this:

var PassThrough = require('stream').PassThrough || require('readable-stream/passthrough')

module.exports = function(isRun) {
  var submissionOut = new PassThrough()
    , solutionOut   = new PassThrough()
  var superagent = require('superagent')
  setTimeout(function() {
    superagent
      .get('http://localhost:3000/home')
      .pipe(submissionOut)
    if (!isRun) {
      superagent
        .get('http://localhost:3001/home')
        .pipe(solutionOut)
    }

  }, 500)

  return {
      submissionArgs: [3000]
    , solutionArgs: [3001]
    , a : submissionOut
    , b : solutionOut
  }
}

To see if this what was actually being invoked, I increased the timeout from 500 to 3000. To my surprise, this was all it took to make the solution verify!! For some reason the connection on my local machine was taking more than half a second! Not sure why.

Please increase the timeout in the source. Thanks.

dtopham75 commented 9 years ago

I'm seeing the same behaviour with this exercise as described in the original post by @amahfouz.

I'm also on Windows 7, with Node 0.10.33. I've tried with both express.js v3.18.4 and v4.10.4, the result is the same.

I see that the timeout in the setup.js file in the exercise has been increased, but this doesn't seem to have made any different in my case. I've tried setting it much higher with no effect. I've also tried pasting directly from the solution.js, but the behaviour is the same.

If I actually point my browser at http://localhost:3000/home, the page is served as expected.

ardooe commented 9 years ago

I solved this by installing the express module in the working directory.

JimmyLv commented 9 years ago

@ardooe

me, too :) $ sudo npm install express $ ls node_modules program.js

brookinc commented 9 years ago

I had the same issue as @amahfouz (I opened the above nodeschool ticket before I found this one and realized the source of the issue). Increasing the timeout resolved it for me as well, although since I'm running a pretty slow setup (under vagrant on an old MacBook), I had to set it to 4500 -- anything less timed out. :(

I also noticed that the recent refactoring from setup.js to exercise.js changed the timeout value from 1500 back to 500 -- although even 1500 would have been too short in my case.

manshell commented 5 years ago

For some reason, it worked with me when I removed the ";" after each script. For example, instead of doing app.listen(process.argv[2] || 3000) I was doing app.listen(process.argv[2] || 3000); It does not make sense, but it worked.