简体   繁体   中英

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 .

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

// 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM