Daniel Cappelletti
Daniel Cappelletti

Reputation: 3

Expresss Problem TypeError: Cannot read property 'method' of undefined

I am starting a project in node and it jumps to me is the problem I cannot find the error

:\Users\RTECH\Desktop\work\aumentedreality\modelViewerwithExpress\node_modules\express\lib\router\index.j debug('dispatching %s %s', req.method, req.url); TypeError: Cannot read property 'method' of undefined

src/index.js:

const express = require("express");
const app = express();
const path = require("path");

//settings
app.set("port", 4000); 
app.set("views", path.join(__dirname , "views")); 
app.set("view engine", "ejs");

//routes
app.use(require("./routes/")); 
//routes

//static file
app.use(express.static(path.join(__dirname , "public")));
//static file

//port
app.listen(
  app.get("port", () => {
    console.log("server run on port: ", app.get("port"));
  })
);
//port

/routes/index.js

const express = require("express");
const router = express.Router();

//creamos routes

router.get("/", (req , res) => {
  
  res.render("index", { title: "realidad aumentada prueba" });
});


module.exports = router;

Upvotes: 0

Views: 331

Answers (2)

Daniel Cappelletti
Daniel Cappelletti

Reputation: 3

Joan Albert answer solved my problem, I leave final file. thanks

const express = require("express");
const app = express();
const path = require("path");
const routes = require("./routes/");


//settings
app.set("views", path.join(__dirname, "views")); //url de entrada de la vista
app.set("view engine", "ejs"); //le decimos el motor de plantilla que usamos
//settings

//routes
app.use(routes); //Le decimos de donde lee las rutas
//routes

//static file
app.use(express.static(path.join(__dirname, "public")));
//static file

//port
app.listen(4000, () => {
  console.log("Listening on port 4000");
});
//port

  //static file
   app.use(express.static(path.join(__dirname, "public")));
  //static file

  //port
   app.listen(4000, () => {
    console.log("Listening on port 4000");
   });
  //port

Upvotes: 0

Joan Albert
Joan Albert

Reputation: 665

I would try to import your routes at the top, as usual, then use it in your middleware.

const routes = require("./routes/");

app.use(routes);

Also the standard nowadays is to set the port directly, not with the app.set("port", 4000); so I would remove that line and changed your app.listen to set it this way instead:

app.listen(4000, () => {
  console.log('Listening on port 4000');
});

Everything else looks good to me.

Upvotes: 1

Related Questions