Search code examples
node.jsservergraphqlapollofastify

Fastify stops in production mode


Hello I want to run fastify with react build files for front-end. I don't know why when I run my server, it stops after running.

yarn run v1.22.10 $ node dist/src/index.js Server is ready at port 4000 Done in 3.03s.

my code:

app.register((instance, opts, next) => {
  instance.register(fastifyStatic, {
    root: path.join(__dirname, "build"),
    prefix: "/*/",
  });
  next();
});

server.start().then(() => {
  app.register(server.createHandler({ path: "/graphql", cors: corsOptions }));
  app.listen(PORT, "0.0.0.0", (err) => {
    if (err) {
      console.error(err);
    } else {
      console.log("Server is ready at port 5000");
    }
  });
});

Solution

  • I found the solution. But I still have access to playground. Will update if I figured it out.

    app.register((instance, opts, next) => {
      instance.register(fastifyStatic, {
        root: path.join(__dirname, "build"),
        prefix: "/",
      });
      instance.setNotFoundHandler((req, res) => {
        res.sendFile("index.html", path.join(__dirname, "build"));
      });
      next();
    });