I searched everywhere but couldn't find the answer to :. Is it possible to add multiple routers on the same path in node.js . Like :-
app.js
var users = require('./routes/users.js');
var users1 = require('./routes/users1.js');
app.use('/' , users);
app.use('/' , users1);
Is the above possible ?! If no , how can the above be modified ? Basically what I am trying to do is , first module displays information from database . When user clicks on ADD button , my other module gets called which is responsible for taking new information and storing it in database .
I am totally new to node.js . Any help would be immensely appreciated Thank you !
degrees.ejs
<form id = 'degrees' method = POST action = '/main/degrees/add'>
<table id = "tg" class="tg" align = "center" style="width: 1041px">
<colgroup>
<col style="width: 80px">
<col style="width: 80px">
<col style="width: 100px">
<col style="width: 321px">
<col style="width: 425px">
<col style="width: 100px">
</colgroup>
<tr>
<th class="tg-yw4l" colspan="2"> Qualification <br/>(fill up like UG | B.TECH)<br><br></th>
<th class="tg-yw4l"> Field of Specialisation </th> </label>
<th class="tg-yw4l"> Institute </th> </label>
<th class="tg-yw4l"> Year of Passing </th> </label>
</tr>
<tr>
<td class="tg-yw4l1" style="width: 200px" contenteditable="true"> <%= Graduation %> </td>
<td class="tg-yw4l1" style="width: 200px" contenteditable="true"> <%= Degree %> </td>
<td class="tg-yw4l1" style="width: 321px" contenteditable="true"> <%= Specialisation %> </td>
<td class="tg-yw4l1" style="width: 525px" contenteditable="true"> <%= Institution %> </td>
<td class="tg-yw4l1" style="width: 250px" contenteditable="true"> <%= Year %> </td>
</tr>
<tr>
<td class="tg-yw4l2"><input type="text" name="Graduation" style="width: 90px"></td>
<td class="tg-yw4l2"><input type="text" name="Degree" style="width: 90px"></td>
<td class="tg-yw4l2"><input type="text" name="Specialisation" style="width: 321px"></td>
<td class="tg-yw4l2"><input type="text" name="Institution" style="width: 425px"></td>
<td class="tg-yw4l2"><input type="number" name="Years" style="width: 100px"></td>
</tr>
</table>
</form>
<button onclick = "add()"> + ADD </button>
</body>
<script type="text/javascript">
function add()
{
var tables = document.getElementById("tg");
var rows = tables.insertRow(-1);
var cell1 = rows.insertCell(0);
var cell2 = rows.insertCell(1);
var cell3 = rows.insertCell(2);
var cell4 = rows.insertCell(3);
var cell5 = rows.insertCell(4);
cell1.innerHTML = "<div contenteditable> </div>";
cell2.innerHTML = "<div contenteditable> </div>";
cell3.innerHTML = "<div contenteditable> </div>";
cell4.innerHTML = "<div contenteditable> </div>";
cell5.innerHTML = "<div contenteditable> </div>";
}
</script>
routes/users.js
var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://<username>:<password>@ds141434.mlab.com:41434/teacherspro';
var _db;
MongoClient.connect(url , function(err,db){
if (err) return;
_db = db;
});
router.post('/' , function (req,res){
_db.collection('project1').find({"UserName" : "karan"} , {Graduation : 1 , Degree : 1 , Specialisation : 1 , Institution : 1 , Year : 1 }).forEach(function(fifa){
console.log('loaded 2 time!');
res.render('degrees' , {Graduation : fifa.Graduation , Degree : fifa.Degree , Specialisation : fifa.Specialisation , Institution : fifa.Institution , Year : fifa.Year});
});
});
module.exports = router;
routes/user1.js
var express = require('express');
var router = express.Router();
var MongoClient = require( 'mongodb' ).MongoClient;
var url = 'mongodb://<username>:<password>@ds141434.mlab.com:41434/teacherspro';
var _db;
MongoClient.connect(url , function(err,db){
if (err) return;
_db = db;
});
router.post('/' , function (req,res) {
res.render('degrees');
var users = {
Graduation : req.body.Graduation.Graduation,
Degree : req.body.Degree,
Specialisation : req.body.Specialisation,
Institution : req.body.Institution,
Year : parseInt(req.body.Year)
};
_db.collection('project1').insertOne({users : users} , function(err){
if(err) {console.log('no insert!');}
console.log('Data inserted!');
});
});
module.exports = router;
It is allowed, but the first handler to match the route and send a response without calling next() will have precedence and the other route handler will not be called. Express matches routes in the order they were declared. The first one to match gets first crack at the route. If it doesn't purposely tell Express to continue routing to other handlers by calling next(), then no other route handler will be called.
If you want routing to continue after a route handler is called, you can call next() to tell Express to continue looking for other route handlers that match the route.
app.use('/' , users);
// then users could be this:
function users(req, res, next) {
if (want to handle route here) {
res.send(someResponse);
} else {
// tell Express to continue routing to other handlers
next();
}
}
So lets assume that I have 1 route rendering my information from database ..if I click ADD button , a new column appears for entering new information , code and details of which are in another module ... What if I don't want to redirect but use the same rendered page again for taking up the new information ?
If you just want to use the same button to repeat the process again, that's no problem. The same route will be hit and will repeat the operation.
If you want to use the same route to implement a different operation, then that's not an appropriate design. A given route should have a given function. If you want to change the function for a different function of your site, then use a different route. Your form post can return new content that offers a different form for the next operation. Of you can use Javascript in the page to change the logic within a page that isn't being reloaded. To help more specifically on this aspect of the question, we'd have to see actual code/html to know what's going on and what exactly to recommend. But, a given route should not do two different things at different times. That's a wrong design.
Keep in mind, you can also use query parameters with a route to communicate additional information to the route handler, though it would again be a poor design to use a query parameter to entirely change the function of the route. A route should work like a function call with arguments and should generally do the same thing every time you call it (subject to the meaning of the arguments you pass it).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With