title: Nextjs and Express as middle ware. How do I set 'localhost:3000/newpage' and 'localhost:3000/newpage/' as the same routes
body: <p>I am new to express and next and was trying to set 'localhost:3000/newpage' and 'localhost:3000/newpage/' as the same route however as I add a '/' at the end it shows a 404 error. </p>
  
  <p>I am using "next-routes" for dynamic routing and have created routes.js file  that looks like this:</p>
  
  <pre><code>const nextRoutes = require("next-routes");
  const routes = (module.exports = nextRoutes());
  
  routes.add("index", "/");
  routes.add("newpage", "/newpage/:slug"); //with body parser this doesnt work
  
  </code></pre>
  
  <p>and my server.js file looks like this:</p>
  
  <pre><code>const express = require("express");
  const next = require("next");
  const routes = require("./routes");
  const dev = process.env.NODE_ENV !== "production";
  const port = process.env.PORT || 3000;
  const app = next({ dev });
  const handle = app.getRequestHandler();
  const bodyParser = require("body-parser");
  const handler = routes.getRequestHandler(app);
  app
    .prepare()
    .then(() =&gt; {
      const server = express();
      server.use(bodyParser.json()); //with this dynamic routes dont work
      server.use (handler); //with this dynamic routes work but / url show 404
  
      server.get("*", (req, res) =&gt; {
        server.use(handler);
        if (req.url.endsWith("/")) {
          req.url = req.url.slice(0, -1); // works only when using body parser
        }
        return handle(req, res);
      });
  
      server.listen(port, (err) =&gt; {
        if (err) throw err;
        console.log("&gt; Ready on http://localhost:3000");
      });
    })
    .catch((ex) =&gt; {
      console.error(ex.stack);
      process.exit(1);
    });
  
  
  
  </code></pre>