question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

request: expose httpServer to app context

See original GitHub issue

I’m submitting a…


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

I want to be able to access the httpServer from the application context. Specifically, in my case I want to simply register a graphql subscriptions server (I saw the example, but it’s too complicated and creates a new server instead of reusing the existing one)

class MyModule implements NestModule {

// ...

configure(consumer: MiddlewaresConsumer) {
  // get schema

    new SubscriptionServer({
      execute,
      subscribe,
      schema
    }, {
      // server: appHttpServer, // ?? how do I get reference to the app server?
      path: '/subscriptions',
    });

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
kamilmysliwieccommented, May 13, 2018

You can inject HTTP server reference using HTTP_SERVER_REF token. The reference is an HTTP adapter currently used by the application. Each adapter implements HttpServer interface:

@Inject(HTTP_SERVER_REF) httpServerRef: HttpServer

The HTTP_SERVER_REF is exported from @nestjs/core while HttpServer interface from @nestjs/common. Also, this instance exposes getHttpServer() method that allows picking up the underlying object (express instance by default).

1reaction
Melsecommented, Jul 20, 2018

@blissi try this.httpServerRef.getHttpServer()

@Injectable()
export class SubscriptionsService implements OnModuleDestroy {
    private subscriptionServer: SubscriptionServer;

    constructor(
        @Inject(HTTP_SERVER_REF) private readonly httpServerRef: HttpServer,
    ) {}

    createSubscriptionServer(
        schema: any,
        options: ServerOptions = {},
        socketOptions: WebSocket.ServerOptions = {},
    ) {
        this.subscriptionServer = new SubscriptionServer(
            {
                execute,
                subscribe,
                schema,
                ...options,
            },
            {
                server: this.httpServerRef.getHttpServer(),
                path: '/subscriptions',
                ...socketOptions,
            },
        );
    }

    onModuleDestroy() {
        this.subscriptionServer.close();
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

HTTP servers — Python 3.11.1 documentation
This class is used to handle the HTTP requests that arrive at the server. By itself, it cannot respond to any actual HTTP...
Read more >
Simple HTTP server in Java using only Java SE API
Umm...the short answer is no. If you want something that handles post and get requests without manually writing the http headers then you...
Read more >
How To Make an HTTP Server in Go
In this section, you'll start by using the function http.HandleFunc to tell the server which function to call to handle a request to...
Read more >
API Reference: ApolloServer - Apollo GraphQL Docs
This article documents the ApolloServer class from the @apollo/server package. You can use the ApolloServer class to create an instance of Apollo Server ......
Read more >
Execution context | NestJS - A progressive Node.js ...
... Nest HTTP server-based, microservices and WebSockets application contexts). ... of the box by the framework and exposed from the @nestjs/core package.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found