0

I have come across a problem that is probably silly but comes from my little knowledge of express. I have created a project with express and I cannot make calls to the root, only to the routes. I am pretty sure the problem lies somewhere in app.use(...).

app.js

var index = require('./routes/index');
var something = require('./routes/something');
...
app.use('/', index);
app.use('/users', users);
//This returns not found
app.get('/log', function(req, res){
   res.send("Test");
};

index.js

//Render index.ejs to localhost
...
//This works
idx.get(/log', function(req, res){
   res.send("Test Index");
}

So what do I write into app.user('/', HERE) in order to make get/post calls work in app.js. I believe app.use(app.router) took care of that in 3.x version.

John Smith
  • 77
  • 10

1 Answers1

0

This is weird, I never faced something like that, I just can tell your some hints. Maybe it is something about the order you've declared the app.js. I got the part below from this topic (thanks Peter Lyons), maybe it can be helpful for you:

•Don't use app.configure. It's almost entirely useless and you just don't need it. It is in lots of boilerplate due to mindless copypasta. •THE ORDER OF MIDDLEWARE AND ROUTES IN EXPRESS MATTERS!!! •Almost every routing problem I see on stackoverflow is out-of-order express middleware •In general, you want your routes decoupled and not relying on order that much •Don't use app.use for your entire application if you really only need that middleware for 2 routes (I'm looking at you, bodyParser) •Make sure when all is said and done you have EXACTLY this order: •Any super-important application-wide middleware •All your routes and assorted route middlewares •THEN app.router •THEN error handlers

•Sadly, being sinatra-inspired, express.js mostly assumes all your routes will be in server.js and it will be clear how they are ordered. For a medium-sized application, breaking things out into separate routes modules is nice, but it does introduce peril of out-of-order middleware

Community
  • 1
  • 1
Ito
  • 2,034
  • 3
  • 22
  • 31