【问题标题】:Fastify stops in production modeFastify 在生产模式下停止
【发布时间】:2021-12-04 21:36:24
【问题描述】:

你好,我想用前端的 react 构建文件运行 fastify。我不知道为什么当我运行我的服务器时,它运行后就停止了。

yarn run v1.22.10 $ node dist/src/index.js 服务器在端口准备就绪 3.03 秒内完成 4000 个。

我的代码:

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");
    }
  });
});

【问题讨论】:

  • 服务器变量是谁?您应该检查侦听回调中的错误参数
  • @ManuelSpigolon 收到错误,但问题是它不会将所有路由重定向到构建文件夹
  • 你能分享堆栈跟踪吗?
  • @ManuelSpigolon 感谢您的耐心等待。我已设法将所有内容重定向到我的反应构建。但现在我对操场有疑问。我仍然可以进入游乐场。

标签: node.js server graphql apollo fastify


【解决方案1】:

我找到了解决方案。但我仍然可以使用游乐场。如果我想通了会更新。

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();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-22
    • 2012-10-11
    • 2023-02-26
    • 2015-06-23
    • 1970-01-01
    • 2020-01-24
    • 2020-03-19
    • 1970-01-01
    相关资源
    最近更新 更多