没有调用NextJS的ExpressJS中间件

时间:2018-03-23 18:22:57

标签: javascript reactjs express nextjs

正如标题所示,我有一个使用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;
  });
});

0 个答案:

没有答案