How do I get the http.server from the express app?

Graeme Perrow

I am writing a server-side node.js application that uses express to handle HTTP requests and I'm adding websockets (socket.io) support. However I don't have access to the code that starts the HTTP server. I am writing an express router and functions to handle various URLs. My code looks like:

var express = require('express');
var router = express.Router();
router.post( myURL, myFunction );

When setting up the router I want to use socket.io to listen for websocket connections as well. I cannot pass express or express() into require('socket.io')( xxx ), I need to pass the http.Server object. But since I didn't start the server, how can I get it?

jfriend00

As far as I know, the Express app object does not know the server object. The way things work in Express, the app object is given to the server object, not the other way around. In fact a single app object can even be used with more than one server (sometimes done for an http and https server).

You can get access to the server object from within a request handler with req.connection.server, but that comes from the server as part of the context with a request, that's not something the app object itself knows.

So, if you want access to the server object for use with socket.io at initialization time, you will have to capture the server object into a variable where it is created.

The code you show in your question does not create or start a server. The usual two ways to start a server for use with Express are this:

var express = require('express');
var app = express();
// Express creates a server for you and starts it
var server = app.listen(80);

Or, you create the server yourself and pass express to it as the handler:

var express = require('express');
var app = express();
// you explicitly create the http server
var server = require('http').createServer(app);
server.listen(80);

In each of these cases, you end up with the server object in a variable and can then use that with socket.io.


You can get access to the server from inside an Express route handler within the processing of a request from either the req or res object that are passed to a request handler.

res.connection.server
req.connection.server

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I invoke protractor from within an express node server?

From Dev

How do I get data to display in Angular from API in Express?

From Dev

In a Nodejs app how do I make an HTTP client request inside an Express method

From Dev

In a Nodejs app how do I make an HTTP client request inside an Express method

From Dev

How do I send an HTTP GET request from a Chrome extension?

From Dev

How do I get an HTTP status code from TTransport exceptions?

From Dev

How do I render data from an HTTP GET on the client (with Handlebars)

From Dev

How do I get HTTP Reason Phrase from SuperAgent/node?

From Dev

How do I get headers from a LightTPD CGI app?

From Dev

How do I get the app log from a real Android device?

From Dev

How do I get the last record from sql server?

From Dev

How do i get a data from JSON server response in PHP

From Dev

How do I get the instance memory in use for a Node-Express App running in CloudFoundry?

From Dev

How do I get CodeMap in VS2013 to run against a SQL Server 2014 Express instance?

From Dev

How do I get the full route path including the parameters from express or extend the request object to do so?

From Dev

How do I separate my REST API from my Express web app?

From Dev

how to get the server name from a http response

From Dev

How do I send an HTTP GET with a body?

From Dev

How do I get intent extras from an app that launches another app?

From Dev

How do I optimise this callback hierarchy in my simple express app?

From Dev

How do I get the PID of this Java app?

From Dev

How Do I Get Into App Development For Android?

From Dev

How do I send large (over 64k) http responses from a java servlet in a jetty server?

From Dev

How can I see the server response from an HTTP GET request in Swift?

From Dev

How do I set up Node/Express to act as a STUN server?

From Dev

How do I set up a NodeJS Express server?

From Dev

In RoR, how do I catch an exception if I get no response from a server?

From Dev

In RoR, how do I catch an exception if I get no response from a server?

From Dev

How do I get internet on my server?

Related Related

  1. 1

    How do I invoke protractor from within an express node server?

  2. 2

    How do I get data to display in Angular from API in Express?

  3. 3

    In a Nodejs app how do I make an HTTP client request inside an Express method

  4. 4

    In a Nodejs app how do I make an HTTP client request inside an Express method

  5. 5

    How do I send an HTTP GET request from a Chrome extension?

  6. 6

    How do I get an HTTP status code from TTransport exceptions?

  7. 7

    How do I render data from an HTTP GET on the client (with Handlebars)

  8. 8

    How do I get HTTP Reason Phrase from SuperAgent/node?

  9. 9

    How do I get headers from a LightTPD CGI app?

  10. 10

    How do I get the app log from a real Android device?

  11. 11

    How do I get the last record from sql server?

  12. 12

    How do i get a data from JSON server response in PHP

  13. 13

    How do I get the instance memory in use for a Node-Express App running in CloudFoundry?

  14. 14

    How do I get CodeMap in VS2013 to run against a SQL Server 2014 Express instance?

  15. 15

    How do I get the full route path including the parameters from express or extend the request object to do so?

  16. 16

    How do I separate my REST API from my Express web app?

  17. 17

    how to get the server name from a http response

  18. 18

    How do I send an HTTP GET with a body?

  19. 19

    How do I get intent extras from an app that launches another app?

  20. 20

    How do I optimise this callback hierarchy in my simple express app?

  21. 21

    How do I get the PID of this Java app?

  22. 22

    How Do I Get Into App Development For Android?

  23. 23

    How do I send large (over 64k) http responses from a java servlet in a jetty server?

  24. 24

    How can I see the server response from an HTTP GET request in Swift?

  25. 25

    How do I set up Node/Express to act as a STUN server?

  26. 26

    How do I set up a NodeJS Express server?

  27. 27

    In RoR, how do I catch an exception if I get no response from a server?

  28. 28

    In RoR, how do I catch an exception if I get no response from a server?

  29. 29

    How do I get internet on my server?

HotTag

Archive