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.

Expose express library to end users?

See original GitHub issue

I would like to serve my build/dist folder for my static assets when developing/deploying to now or any server. For that I need to install express again and use the static function from there. It would be great if graphql-yoga had this inbuilt for my static assets otherwise it would good enough to re-export the express library for public use.

This is what I’m doing now

import express from 'express';
server.express.use(express.static('dist'));

Something like this would be more easier,

import GraphQLYoga from 'graphql-yoga';
const { GraphQLServer, express } = GraphQLYoga;

const server = new GraphQLServer({ typeDefs, resolvers, options })
server.express.use(express.static('dist'));

else more like this,

const server = new GraphQLServer({ typeDefs, resolvers, options: {
  static: 'dist',
 }});

BTW Great library. I had the same amount of code throughout my projects. This unifies it and also the apollo-upload-server is a plus that too it got recently released too.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
newtmexcommented, Jan 23, 2019

@geminiyellow this worked for me

// Serve static files
server.express.use(express.static(path.join(__dirname, '../client/build')));
// Serve other routes to index...typical for Angular frontend app
server.express.get('*', (req, res, next) => {
   // Handle graphql-yoga specific routes
   if(req.url == options.playground ||
    req.url == options.subscriptions ||
    req.url == options.endpoint) {
      // Return next() so that the GraphQLServer will handle it
      return next();
    }
   res.sendFile(path.join(__dirname, '../client/build/index.html'))
});

I know this issue is closed, but I felt like contributing a solution that worked for me.

0reactions
Urigocommented, Mar 29, 2022

Hey, @Urigo from The Guild here!

You might know us from projects such as graphql-code-generator, envelop or graphql-tools.

For a long time we thought that the Javascript ecosystem is still missing a lightweight cross-platform, but still highly customizable GraphQL Server.

In the past the awesome Prisma team took on that great challenge and now we are happy to announce that we are continuing them and just released GraphQL Yoga 2.0 - Build fast, extensible, and batteries-included (Subscriptions, Serverless, File uploads support) GraphQL APIs in Node.js 🚀

And regarding the issue here, Yoga is completely separated from the way you do HTTP, so you can easily use and expose anything to express but also use Yoga on many other environments and frameworks like Fastify, Cloudflare Workers, Lambdas and many more (you can find all of them in the docs)

We have been working a long time on version 2.0 and have been using it in our clients projects for a few months now and shared a couple of alpha cycles here. Thank you all for your feedback and suggestions, you made this release possible!

Please try Yoga out again, give us feedback and help us spread the word on the new release!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expose express library to end users? · Issue #9 - GitHub
I would like to serve my build/dist folder for my static assets when developing/deploying to now or any server. For that I need...
Read more >
Exposing your local Node.js app to the world | Pluralsight
Learn how to expose your Node.js app in Localtunnel.me, ngrok, Zeit's now using a Hello World Express app. See which service may better...
Read more >
Express/Node introduction - Learn web development | MDN
A module is a JavaScript library/file that you can import into other code using Node's require() function. Express itself is a module, as...
Read more >
Node.js and Express Tutorial: Building and Securing RESTful ...
TL;DR: In this article, you will learn how to develop RESTful APIs with Node.js, Express, and Auth0. You will start from scratch, scaffolding...
Read more >
Session Management in Node.js using ExpressJS and ...
Setting up the session middleware · Import all the Node.js libraries that we explained earlier · Initialize the express app · Add the...
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