Reputation: 251
So I have two routes where after each route you can type an ID, and get the specific ID of that restaurant (ignore all the misspellings.. I know) back,
If I type /all it's like it's trying to get /all as an ID..
I'm sure it's something small and I know it is weird to get an ID from being on both routes, but.. whatever, I am new to Express, and well JS an all only about 8 weeks into coding in general.
resaruants.js route -
const express = require('express'),
router = express.Router(),
restaruantModel = require('../models/restaruantModel');
router.get('/:rid?', async (req, res) => {
const { rid } = req.params;
let restaruantData = [];
if (!!rid) {
restaruantData = await restaruantModel.getById(rid);
} else {
restaruantData = await restaruantModel.getAll();
};
res.render ('template', {
locals: {
title: 'Restaruants',
dataArray: restaruantData
},
partials : {
partial: 'partial-index'
}
});
});
module.exports = router;
allTheRestaruants.js (Spelling is hard) - router :
const express = require('express'),
router = express.Router(),
restaruantModel = require('../models/restaruantModel');
router.get('/:id?', async (req, res) => {
const { id } = req.params;
let restaruantData = [];
if (!!id) {
restaruantData = await restaruantModel.getById(id);
} else {
restaruantData = await restaruantModel.getAll();
};
res.render ('template', {
locals: {
title: 'All The Restaruants',
dataArray: restaruantData
},
partials : {
partial: 'partial-all'
}
});
});
module.exports = router;
I've narrowed the code down.. I am pretty sure to being in my routes.
TLDR ;
/all route doesn't work, reading "all" as an ID from the index. Searching by the ID does work tho.
Upvotes: 0
Views: 579
Reputation: 75073
You can't have the same route with the same option resaruants.js
route is exactly the same as allTheRestaruants.js
it would only worked if:
const resRoute = require('./routes/resaruants')
const resAllRoute = require('./routes/allTheRestaruants')
...
app.use('/restaurant', resRoute)
app.use('/restaurant-all', resAllRoute)
giving you 2 routes.
But, I'm assuming you're trying to do a REST API, the idea is that, when you pass no id
you automatically get all, so no need to specify all, for example:
a simple restaurants.js
routing file
const router = require('express').Router();
const restaruantModel = require('../models/restaruantModel');
const sendData = (res, dataArray, title) => {
res.render ('template', {
locals: {
title,
dataArray
},
partials : {
partial: 'partial-all'
}
});
}
const getAll = async (req, res) => {
const data = await restaruantModel.getAll();
sendData(res, data, 'All The Restaruants')
}
const getById = async (req, res) => {
const { id } = req.params;
const data = await restaruantModel.getById(id);
sendData(res, data, 'Restaruant #' + id)
}
router.get('/restaurants', getAll)
router.get('/restaurants/:id', getById)
module.exports = router;
it is easier to read, and you do something that you should always aim: separation of concerns
this way, a GET /restaurants
will get you all (you better add paging as well later on) and GET /restaurants/23
will get only the restaurant #23...
BTW, if you use Visual Studio, as English is not my mother language, and I love to write code in English so all can read property, I use Code spell checker plugin :)
Upvotes: 1