1

正如标题所示,我有一个使用 ExpressJS 作为自定义服务器的 next.js 应用程序。我设置了一个简单的中间件来做一些日志记录,但它根本没有被调用。

const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  const server = express();
  server.use(helmet());
  server.use(bodyParser.json());

  // >>> this is not being called <<<
  server.use(function(req, res, next) {
    console.log("LOGGED");
  });

  server.get("/", (req, res) => {
    const indexPage = "/";
    app.render(req, res, indexPage);
  });
  server.get("/about", (req, res) => {
    const aboutPage = "/about";
    app.render(req, res, aboutPage);
  });
  server.post("/search", [
    checkSpotifyToken,
    async (req, res) => {
      const { selectedMovie } = req.body;
      try {
        const playlistId = await buildPlaylist(selectedMovie);
        res.json({ success: true, playlistId });
      } catch (error) {
        console.log(error);
        res.json({ success: false });
      }
    }
  ]);
  server.get("*", (req, res) => {
    return handle(req, res);
  });
  server.listen(3001, err => {
    if (err) throw err;
  });
});
4

0 回答 0