[英]Cannot import a function in another file (node.js)
I have an issue that I can't resolve.我有一个无法解决的问题。 I'm working with node.js and I simply want to use an export router function in another file .我正在使用 node.js,我只想在另一个文件中使用导出路由器功能。
Here is the code from the import file:这是导入文件中的代码:
import express from "express";
import * as zoneInstance from "../../controllers/objectInstance/zoneInstance.js";
import { mobilityRouter } from "../../controllers/routerFunction/routerLogic.js";
const router = express.Router();
/* Router for mobility render
// const mobilityRouter = (zone, instance) => {
// return (
// router.get("/mobility/" + zone, instance)
// );
// } */
mobilityRouter(zoneInstance.shoulder.zone, zoneInstance.shoulder.mobilityRender());
Here is the code from the export file:这是导出文件中的代码:
import express from "express";
const router = express.Router();
// Router for mobility render
export const mobilityRouter = (zone, instance) => {
return (
router.get("/mobility/" + zone, instance)
);
}
// Router for reinforcement render
export const reinforcementRouter = (zone, instance) => {
return (
router.get("/reinforcement/" + zone, instance)
);
}
// Router for proprioception
export const proprioceptionRouter = (zone, instance) => {
return (
router.get("/proprioception/" + zone, instance)
);
}
In the index.js在 index.js 中
// Routes for
import mobilityRouter from "./routes/mobility/mobilityAPI.js";
const app = express();
//for mobility URL
app.use("/", mobilityRouter);
When I copy past the 'mobilityRouter' function and use it in the import file it works perfectly, but once I put it in another file and exports it I have a "cannot GET/" in my browser.当我复制过去的 'mobilityRouter' 函数并在导入文件中使用它时,它可以正常工作,但是一旦我将它放入另一个文件并导出它,我的浏览器中就会出现“无法获取/”。 Thank you for your help.感谢您的帮助。
You have different instances of router in both files.您在两个文件中都有不同的路由器实例。 Either pass router as a parameter to the function in the other file or export & import in your routerLogic.js.要么将 router 作为参数传递给另一个文件中的函数,要么在 routerLogic.js 中导出和导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.