Skip to content

Bug with the router class #6171

Answered by krzysdz
evokerking1 asked this question in Q&A
Discussion options

You must be logged in to vote

The router's constructor (Router()) is a part of the "express" module, not the instance of the app.

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/`);
    c…

Replies: 8 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies

This comment was marked as off-topic.

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@IamLizu
Comment options

Answer selected by IamLizu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
6 participants
Converted from issue

This discussion was converted from issue #6034 on November 12, 2024 12:01.