Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Make it compatible with express #11

Closed
kiddyfurby opened this issue Apr 1, 2014 · 6 comments
Closed

Make it compatible with express #11

kiddyfurby opened this issue Apr 1, 2014 · 6 comments
Labels

Comments

@kiddyfurby
Copy link

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

@rubenv
Copy link
Owner

rubenv commented Apr 1, 2014

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.

@rubenv rubenv added the notabug label Apr 1, 2014
@kiddyfurby
Copy link
Author

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

@kiddyfurby
Copy link
Author

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?

@rubenv
Copy link
Owner

rubenv commented Apr 1, 2014

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" [email protected] 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
.

@drinchev
Copy link

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

@mk-pmb
Copy link
Contributor

mk-pmb commented Nov 9, 2016

it should be in the README file.

👍
Available in PR #14.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants