rubenv/node-systemd

Make it compatible with express

kiddyfurby opened this issue · 6 comments

I tried integrating systemd socket activation with express app,

so in my express app.js,

require('systemd')
...
var http = require('http')
var port = process.env.LISTEN_PID > 0 ? 'systemd' : process.env.PORT || 4000
http.createServer(app).listen(port);
console.log('Express app started on port '+port);

I received the following error,

Express app started on port systemd
Apr 01 16:58:52 : events.js:72
Apr 01 16:58:52 : throw er; // Unhandled 'error' event
Apr 01 16:58:52 : ^
Apr 01 16:58:52 : Error: listen ENOTSOCK
Apr 01 16:58:52 : at errnoException (net.js:904:11)
Apr 01 16:58:52 : at Server._listen2 (net.js:1042:14)
Apr 01 16:58:52 : at Server.listen (/www/.../node_modules/systemd/lib/systemd.js:17:14)

I digged around a bit but have no idea, it seems that process.env.LISTEN_FDS is mysteriously gone in systemd.js

Here's what I do:

require('systemd');

var app = express();
app.get('/test', function(req, res) {
  return res.json('ok');
});
app.listen(process.env.LISTEN_PID > 0 ? 'systemd' : 3000);

This works, so it should be compatible with express.

yes it works with the above js.
sorry for confusing, i will dig further into my app to see where the incompatibility arose

Found the issue: npm start node server.js

Let me document it here in hope of helping others
It happens that i must run "node server.js" directly from systemd service file
My original config that didn't work was:

  "scripts": {
    "start": "NODE_PATH=./app/controllers NODE_ENV=production node server.js"
  },

and my systemd service file was:

[Service]
ExecStart=/usr/bin/npm start
WorkingDirectory=/www/[site_dir]/

I guess npm is not passing the envs over to node instance?

That's exactly the problem: npm isn't aware of the socket so it cannot hand
it over.

Thanks for diving into this.
On Apr 1, 2014 6:55 PM, "kiddyfurby" notifications@github.com wrote:

Found the issue: npm start node server.js

Let me document it here in hope of helping others
It happens that i must run "node server.js" directly from systemd service
file
My original config that didn't work was:

"scripts": {
"start": "NODE_PATH=./app/controllers NODE_ENV=production node server.js"
},

and my systemd service file was:

[Service]
ExecStart=/usr/bin/npm start
WorkingDirectory=/www/[site_dir]/

I guess npm is not passing the envs over to node instance?

Reply to this email directly or view it on GitHubhttps://github.com//issues/11#issuecomment-39229919
.

Wow, guys this took me ages to find. Thanks. Maybe it should be in the README file.

it should be in the README file.

👍
Available in PR #14.