LearnBoost / up

Zero-downtime reloads and requests load balancer based on distribute.
540 stars 73 forks source link

Up and Express? #8

Closed snsparrish closed 12 years ago

snsparrish commented 12 years ago

Will 'up' work with 'express'? I keep getting an error whenever a request is made: http.js:562 throw new Error("Can't render headers after they are sent to the client.")

Thanks, Shawn

rauchg commented 12 years ago

Yes

$ mkdir /tmp/woot
$ cd /tmp/woot
$ npm install express up
$ cat > server.js

(paste)

var app = require('express').createServer();
app.get('/', function (req, res) { res.send('<marquee><h1>hello snsparrish</h1></marquee>'); });
module.exports = app;

(press ctrl+c)

up server.js

skylamer commented 12 years ago

Module supplied "_the_file_here.js" does not export a valid http.Server.

fix ?

rauchg commented 12 years ago

?

malkomalko commented 12 years ago

same problems happening for me.. anytime I use the programatic interface and not the cli.

0.6.10 and newest npm...

I'll post a log shortly

zmsmith commented 12 years ago

@malkomalko I'm interested in using up from within an express app as well. Haven't written any code yet, but will let you know if I have any success. I know you haven't gotten it working yet, but any gotchas early on I should know about?

Thanks

rauchg commented 12 years ago

Did you guys try the example I posted in the comment above?

zmsmith commented 12 years ago

It looks like the example above requires you to use the up command to start your program. I'm interested in simulating this example:

var up = require('up')
  , master = http.Server().listen(3000)

// initialize up
var srv = up(master, __dirname + '/server');

process.on('SIGUSR2', function () {
  srv.reload();
});
rauchg commented 12 years ago

Oh. The CLI program uses that API internally so it obviously has to work for that example to work.

rauchg commented 12 years ago

In your case, maybe your file is not called /server so you have to tweak that.

zmsmith commented 12 years ago

I'll check it out, thanks.

dbainbridge commented 12 years ago

For Express 3.0x

var express = require('express');
var app = express();
var server = app.listen();

app.get('/', function (req, res) { res.send('<marquee><h1>hello snsparrish</h1></marquee>'); });
module.exports = server;
jameswyse commented 12 years ago

If anyone comes across the error in the OP:

http.js:562
throw new Error("Can't render headers after they are sent to the client.")

It's possibly because you're trying to use your app's http server as the master server. You should create another server for master as all it does is handle proxying to the worker processes.

In my express project I created two files: server.js and up.js. server.js starts (and exports) my express server like above. up.js starts the up master server.

var up = require('up'),
    http = require('http');

var options = {
    numWorkers: 2,
    workerTimeout: '500ms',
    assumeReady: true
};

var master = http.Server().listen(4000);
var srv = up(master, __dirname + '/server', options);

process.on('SIGUSR2', function () {
  srv.reload();
});