Serving over HTTP Using Node.js
Learn how to serve GraphQL HTTP using Node.js.
HTTP is the mainstream choice for client-server protocol due to its simplicity. In this lesson, we’ll review a few best practices for setting up a GraphQL server to operate over HTTP.
Web request
Modern web frameworks use a pipeline model where requests are passed through a middleware stack, which are also called filters or plugins. As the request is processed through the framework pipeline, the server can reveal the request sent by the client and transform, override, or close it with a response. Given how the underlying web framework works, GraphQL should ideally be placed after the authentication middleware. This is so it can access our framework’s unique session and our user information in our HTTP endpoint handlers.
URIs and routes
REST is commonly associated with HTTP. In the previous lesson, we already learned about the difference between REST and GraphQL. REST groups business domains around resources. In contrast, GraphQL is grouped by URLs so that a GraphQL server works on a single URL or endpoint, usually /graphql
.
HTTP methods, headers, and body
Our GraphQL HTTP server should handle and allow the HTTP GET and POST methods.
GET request
When receiving an HTTP GET request, the client should specify the GraphQL query in the “query” string. For example, suppose we want to execute the following GraphQL query:
Get hands-on with 1400+ tech skills courses.