Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在节点/快速路由应用程序中,如何将ES6模块与app.get一起使用?_Javascript_Node.js_Express_Ecmascript 6_Es6 Modules - Fatal编程技术网

Javascript 在节点/快速路由应用程序中,如何将ES6模块与app.get一起使用?

Javascript 在节点/快速路由应用程序中,如何将ES6模块与app.get一起使用?,javascript,node.js,express,ecmascript-6,es6-modules,Javascript,Node.js,Express,Ecmascript 6,Es6 Modules,我决定在NodeJS/Express项目中使用新的ES6导出,而不是使用模块导出。我正在阅读MDN文档,它说导出是这样使用的: export function draw(ctx, length, x, y, color) { ctx.fillStyle = color; ctx.fillRect(x, y, length, length); 在这里,我试图在这个app.get函数中以同样的方式使用它,但是我的编辑器抛出了一个语法错误。我应该使用不同的格式吗我基本上是在尝试将路由容器分

我决定在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()
的返回值无效

导出函数:

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

还有一个后续问题吗?假设我有路由器post函数在第一个答案中使用的依赖项。我是否可以在导出router.post模块的文件中包含这些依赖项而不是将依赖项放在原始导出的模块文件中?我所说的依赖关系是指像(const、requirefs等)这样的依赖关系不会在模块之间泄漏。您必须将值导入到使用它的范围中。您试图在那里导出什么类型的绑定?您希望如何导入和使用该路由?什么是
app
(即它来自哪里)?