2

im trying to build this simple CMS with Restfull routes in Express.js ,

it was working just fine . then i tried to change it a lil bit so that my routes get neater(or not) .so i moved router.get('/new') route below router.post('/') route and it just stopped working as normal. when i try to get /new route the request goes to router.get('/')

this is the related part of my app.js (removed the unnessary parts)

var express=require('express'),
blogRoutes =require('./routes/blogs.js'),
app        =express();

app.use('/blogs',blogRoutes);

and this is my blogs route in blogs.js file (removed unnessary parts)

var express = require('express'),
    router  = express.Router();


router.get('/',function(req,res){...});
router.get('/:id',function(req,res){...});

router.post('/',function(req,res){...});
router.get('/new',function(req,res){...});

router.get('/:id/edit',function(req,res){...});
router.put('/:id',function(req,res){...});
router.delete('/:id',function(req,res){...});

so i want to know how does this work cuz i did not find anything usefull about the order of routes other than the RESTfull routes images on net that new is before create routes.yet it does make any sense why it can't process it correctly .

ItsJay
  • 188
  • 4
  • 17
  • 1
    Possible duplicate of [Order of router precedence in express.js](https://stackoverflow.com/questions/32603818/order-of-router-precedence-in-express-js) – m90 Oct 09 '19 at 18:43

2 Answers2

3

From Express docs:

A route will match any path that follows its path immediately with a “/”.

For example: app.use("/apple", ...) will match “/apple”, “/apple/images”,“/apple/images/news”,

and so on.

It is a healthy approach to move router.get('/',function(req,res){...}); to the bottom.

If you move router.get('/:id',function(req,res){...}); below router.get('/new',function(req,res){...}); that should fix your problem. It is good idea to put router.get('/:id' before router.get('/:id/edit'.

Community
  • 1
  • 1
Ömürcan Cengiz
  • 2,085
  • 3
  • 22
  • 28
  • unfortunately its not about the position of root route i have root route right on the top and /new works just fine below it but the moment i move /new below /:id the requests goes to root route . i made other test routes like /test and it was just the same /test works just fine but the moment its relocated below /:id route it behaves just like wt im dealing with in /new case . – ItsJay Oct 09 '19 at 21:01
  • *as you said i also put router.get('/',function(req,res){}); at the bottom of my code but didnt solve the issue with /new abnormal behaviour below /:id – ItsJay Oct 09 '19 at 21:09
  • 2
    The expressjs router uses [path-to-regex](https://github.com/pillarjs/path-to-regexp) to match the incoming request path against defined routes. /:id is a variable route and the regex will match for all requests with a single slash followed by a value. As /new has the same path length, the order of definition is relevant. – Korbinian Kuhn Oct 09 '19 at 21:27
0

Try moving router.get('/',function(req,res){...}); to the bottom of the list, that's the root router.

Beto
  • 772
  • 2
  • 12
  • 26
  • unfortunately its not about the position of root route i have root route right on the top and /new works just fine below it but the moment i move /new below /:id the requests goes to root route . i made other test routes like /test and it was just the same /test works just fine but the moment its relocated below /:id route it behaves just like wt im dealing with in /new case . – ItsJay Oct 09 '19 at 21:02
  • *as you said i also put router.get('/',function(req,res){}); at the bottom of my code but didnt solve the issue with /new abnormal behaviour below /:id – ItsJay Oct 09 '19 at 21:08