我有一个路由器,对于某些变量,它会保持params未定义。这些特定的变量在我的数据库中被定义为某些东西,但我不是在查询它,所以我不知道最近发生了什么。例如:
router.get('/:variable', function(req,res) {
console.log(req.params.variable);
})
如果我将'85268'设置为变量并且它存在于我的数据库中,它将控制日志未定义。但'85267'不是,它会正常控制。但正如你所看到的,我甚至不是用这个变量查询数据库,但似乎它以某种方式链接。我正在使用Mongoose来模拟我的模式。
编辑:以下是一些代码文件:
app.js
var express = require('express');
var app = express();
var port = process.env.PORT || 8081;
var index = require('./routes/index');
var sites = require('./routes/sites');
var all = require('./routes/all');
var mongoose = require('mongoose');
var redirect = require('./routes/redirect');
app.engine('html',require('ejs').renderFile);
app.set('view engine', 'html');
mongoose.connect('mongodb://localhost/data2');
app.use('/', index);
app.use('/new',sites);
app.use('/all',all);
app.get('/delete',function(req,res){
var urlModel = require('./dbstuff/url.js');
urlModel.remove({},function(err,info){
if (err) throw err;
else{
res.send("All deleted");
}
});
})
app.use('/short/:id',redirect);
app.listen(port);
sites.js
var express = require('express');
var router = express.Router();
var url = require('url');
var validator = require('validator');
var mongoose = require('mongoose');
var urls = require('../dbstuff/url');
var bodyParser = require('body-parser');
router.get('/*', function(req,res){
var parsed = url.parse(req.path).path.slice(1);
console.log(parsed);
if(validator.isURL(parsed,{protocols:['http','https'],require_protocol:true})){
urls.find({original: parsed},{_id:0, shortened: 1, original: 1},function(err,found){
if(err) throw err;
else{
if(found.length == 0){
var newModel = new urls();
var newID = Math.floor(Math.random()*(99999))+1;
newModel.shortened = "https://urlshortener-gkt5015.c9users.io/short/"+newID;
newModel.original = parsed;
newModel.save(function(err,data){
if(err) throw err;
res.json({ original:data.original, shortened:data.shortened,});
})
}
else{
res.json(found[0]);
}
}
});
}
else{
res.send("False");
}
})
module.exports = router;
redirect.js
var express = require('express');
var router = express.Router({mergeParams: true});
var mongoose = require('mongoose');
var url = require('url');
router.get('/',function(req,res){
console.log("using the redirect path.....")
var link = "https://" + req.hostname + req.originalUrl;
console.log(req.params);
console.log(link);
res.send(link);
module.exports = router;