Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

external routing is not working in express js

here is my main.js file

import express from 'express';


// route file are import here
import router from "./user-route/admin-route.js";

// **************** global variable are define here

const app = express();
const port = 5000;

app.use(express.json());
app.use(("./user-route/admin-route.js"));
///  ***************** routing are created are here

app.get("/", (req, res) => {
  res.send("Hello from the server");
});

// ******************  server is created here

app.listen(port, () => {
  console.log("Server is Ready and Running on Port 5000");
});

and here is my external routing file

import express from 'express';

const router = express.Router();


const admin = (req, res, next) => {
    res.send("Hlo from the dashboard");
}

/// admin routers are defined here 


router.route("/admin").get(admin);

export default router;

how I can connect the external routing with main.js file. here I am using the module method.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if i try with require method than it’s work properly. i am not sure but i think problem is here

app.use(("./user-route/admin-route.js"));

>Solution :

Yes, this is the issue:

app.use(("./user-route/admin-route.js"));

You can’t pass a filename to app.use() and expect it to work (in fact, it throws an error).

But you were close:

app.use(router);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading