Bug with the router class #6171
-
This is my code // ... // bind it as an express middleware app.listen(3000) which throws this error $ bun run index.js Bun v1.1.30 (Linux x64) |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments
-
Whats require-triage label? |
Beta Was this translation helpful? Give feedback.
-
@evokerking1 The Triage Process indicates that it means further triaging is needed to properly classify the issue. |
Beta Was this translation helpful? Give feedback.
-
You wrote const router = app.Router() |
Beta Was this translation helpful? Give feedback.
-
That is all my code. |
Beta Was this translation helpful? Give feedback.
-
Hey @evokerking1 👋 The error occurs because app and Router need to be initialized with const app = express()
const router = app.Router() I hope it resolves your issue. |
Beta Was this translation helpful? Give feedback.
-
Agree! this should work const router = app.Router() |
Beta Was this translation helpful? Give feedback.
-
i tried that stuff but it stills throws an error |
Beta Was this translation helpful? Give feedback.
-
The answers above give only partially correct answers. The router's constructor ( const express = require("express"); // the express module
const app = express(); // instantiate the express app; note the `()` at the end
const router = express.Router(); // Router constructor is a part of the module (`express`), not `app`;
// `()` also required
// This demo mounts a router `router` (with a simple GET handler on `/`) on `/r`
router.get("/", (req, res) => res.send("ok"));
app.use("/r", router);
const PORT = 8080;
const server = app.listen(PORT, async () => {
const r = await fetch(`http://127.0.0.1:${PORT}/r/`);
console.log(r.status, await r.text());
server.close();
}); You can also find some examples in the documentation. |
Beta Was this translation helpful? Give feedback.
Hey @evokerking1 👋
The error occurs because app and Router need to be initialized with
express()
andexpress.Router()
respectively, not just referenced.I hope it resolves your issue.