Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 378
Graeme Perrow Avatar asked Jun 27 '16 20:06

Graeme Perrow


People also ask

Is Express server HTTP server?

Express is made on top of the HTTP module. HTTP module provides various tools (functions) to do things for networking like making a server, client, etc. Express along with what HTTP does provide many more functions in order to make development easy.

What is Express Web server?

Express is a web application framework for Node. js that allows you to spin up robust APIs and web servers in a much easier and cleaner way. It is a lightweight package that does not obscure the core Node. js features. In this article, you will install and use Express to build a web server.

How do I get https in Express?

Go to the terminal and run the following command. After creation adds key & cert file in your code, and pass the options to the server. const express = require('express'); const https = require('https'); const fs = require('fs'); const port = 3000; var key = fs. readFileSync(__dirname + '/../certs/selfsigned.


2 Answers

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 
like image 57
jfriend00 Avatar answered Oct 14 '22 08:10

jfriend00


The server is returned when you call app.listen(). For example:

const server = app.listen(process.env.NODE_PORT, () => {   console.log('Listening', server.address()); }); 
like image 44
Brad Avatar answered Oct 14 '22 09:10

Brad