Node/Express 路由应用程序中的 ES6 模块如何与 app.get 一起使用?

How are ES6 modules used with app.get in a Node/Express routing application?

我决定在 NodeJS/Express 项目中使用新的 ES6 导出而不是使用模块导出。我正在阅读 MDN 文档,它说导出是这样使用的:

export function draw(ctx, length, x, y, color) {
  ctx.fillStyle = color;
  ctx.fillRect(x, y, length, length);

在这里,我试图在这个 app.get 函数中以相同的方式使用它,但我的编辑器抛出了语法错误。我应该使用其他格式吗? - 我实际上是在尝试将路由容器分离到单独的文件中进行组织 - 然后将它们导入回我的主 app.js 文件,最后用 express.

进行路由声明
 export app.post('/exampleroute', async (req, res) => {
   ...
 });

// Error: Declaration or Statement expected.

您必须导出一个(默认值或命名变量)。

app.post() 的 return 值没有用。

导出函数:

export const myRouteHandler = async (req, res) => {
   ...
};

然后:

import { myRouteHandler } from "./myModule";
app.post('/exampleroute', myRouteHandler)

或者,导出路由器:

import express from 'express';
export const router = express.Router();

router.post('/exampleroute', async (req, res) => {
   ...
});

然后导入并使用:

import { router } from "./myModule";
app.use("/", router);