我正在学习使用平均堆栈并尝试构建网址缩短器。我有一个模块,它接受req.params.UserUrl检查,并确保它是一个有效的网址,然后创建一个我想用作短路线的随机数。我似乎无法找到保存随机数的方法,以便我可以检查他们的下一个网址请求。谷歌搜索后似乎最有效的方法是使用long_url和short_url:randomNumber保存数据库中的对象。我的代码没有抛出任何错误,但是当我检查我的heroku数据库时,它有一个新条目,但只有ineid生成的_id和__v。有人能告诉我哪里出错了。
路线档案
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var URLShortener = require(process.cwd()+'/public/Modules/urlShortener.module.js');
var ShortURL = require('../models/shortUrl.js');
router.get('/', function(req, res) {
res.render('index', { title: 'FreeCodeCamp Projects' });
});
router.get('/urlShortener', function(req, res){
res.render('freecodecamp/urlShortener', { title: 'Url Shortener Site'});
});
router.get('/urlShortener/:userUrl', function(req, res){
if(URLShortener.checkValidUrl(req.params.userUrl))
{
var UserUrl = req.params.userUrl;
var randNbr = URLShortener.assignRanNbr();
ShortURL.create(URLShortener.createUrlObj(UserUrl, randNbr), function (err, smallUrl) {
if (err) return console.log(err);
else res.json(smallUrl);
});
}
else
{
res.send('Invalid url');
}
});
router.get('/:short', function(req, res){
if(randNbr == req.params.short)
{
res.redirect(userUrl);
}
else
{
res.send('Not the correct shortcut');
}
});
module.exports = router;
Url Schema
var mongoose = require('mongoose')
var Schema = mongoose.Schema
var shortUrlSchema = new Schema({
long_id:String,
short_id:Number
}, {collection: 'shortUrl'});
module.exports = mongoose.model('shortUrl', shortUrlSchema);
urlShortener模块
'use strict'
module.exports.checkValidUrl = function(url){
var pattern = new RegExp(/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/);
return pattern.test(url);
}
module.exports.assignRanNbr = function(){
var randNbr = Math.floor(Math.random() * (9999 - 1 + 1)) + 1;
return randNbr;
}
module.exports.createUrlObj = function(url, num){
var urlObj = {};
urlObj.original_url = url;
urlObj.short_url = 'https://rawlejuglal-me-rawlejuglal-1.c9users.io/freecodecamp/'+num;
return urlObj;
}
答案 0 :(得分:1)
您的createUrlObj方法返回的属性为@charset "utf-8";
body {
background-image: url(http://wallpapercave.com/wp/U7rb02o.jpg);
background-size: cover;
left: 0px;
position: relative;
margin: 0px;
overflow: hidden;
top: 0px;
right: 0px;
bottom: 0px;
}
.menu {
background-image: url((https
://image.freepik.com/free-vector/white-cubes-pattern_1053-248.jpg))
background: url("https://image.freepik.com/free-vector/white-cubes-pattern_1053-248.jpg");
background-repeat: repeat;
background-position: left top;
background-image: url(http://wallpapers.ae/wp-content/uploads/2014/10/Black-Pattern-Background-Wallpaper.jpg);
height: 100%;
width: 300px;
position: fixed;
left: -300px;
cursor: pointer;
}
.icon {
padding-top: 25px;
padding-right: 0px;
padding-left: 25px;
padding-bottom: 25px;
width: 75px;
cursor: pointer;
height: 40px;
z-index:999;
position:absolute;
top:30px;
background:red;
}
和<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="body">
<div class="icon"></div>
</div>
<div class="menu"></div>
,但您的shortUrlSchema属性为original_url
和short_url
。 create方法中的属性名称需要与您的架构匹配。属性值类型还必须与您的模式类型匹配(当前long_id
是一个字符串,short_id
是一个数字)。我认为你真正想要的是你的createUrlObj方法
short_url
和您的架构
short_id
此外,您的module.exports.createUrlObj = function(url, num){
var urlObj = {};
urlObj.long_url = url;
urlObj.short_id = num;
return urlObj;
}
路由应该调用数据库,因为在该路由中未定义var shortUrlSchema = new mongoose.Schema({
long_url: String,
short_id: Number
}, {collection: 'shortUrl'});
和'/:short'
变量。
randNbr