어디에서나 검색했지만 답변을 찾을 수 없습니다. node.js의 동일한 경로에 여러 라우터를 추가 할 수 있습니까? 마찬가지로 : -같은 경로에있는 여러 라우터
는 app.js
var users = require('./routes/users.js');
var users1 = require('./routes/users1.js');
app.use('/' , users);
app.use('/' , users1);
위 수 있습니까?! 아니라면 위의 사항을 어떻게 수정할 수 있습니까? 기본적으로 내가하려는 것은 첫 번째 모듈은 데이터베이스의 정보를 표시하는 것입니다. 사용자가 ADD 버튼을 클릭하면 새로운 정보를 가져 와서 데이터베이스에 저장하는 다른 모듈이 호출됩니다.
전적으로 node.js를 처음 사용했습니다. 어떤 도움이라도 대단히 감사하겠습니다. 감사합니다!
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>
노선/
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;
노선/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;
시도해 보셨습니까? 오류가 있습니까? –
예, 다음 경로가 호출되지 않습니다 –
쓰기는 가능하지만 작동하지 않을 수 있습니다. 경로 지정 계층이 있습니다. 두 번째는 절대로 실행되지 않습니다. –