如何使用Node.js重定向到Google之类的其他页面? [英] How do I redirect to another page like google using Nodejs?

查看:96
本文介绍了如何使用Node.js重定向到Google之类的其他页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取一个将存储在short变量中的数字.然后,我在数据库中查找与该数字匹配的特定shortUrl.如果找到它,我希望响应为完整的URL,那么假设为www.google.com.

I am trying to get a number that will be stored in short variable. Then I look up on the database for a specific shortUrl that matches that number. If I find it I want the response to be the full url, let's suppose www.google.com.

因此,当用户键入例如localhost:8888/3451时,该用户将重定向到www.google.com.br

So when the user type, for instance: localhost:8888/3451 that would redirect to www.google.com.br

app.get('/:short', function(req, res){
    var short = req.params.short;
    shortUrl.find({shortUrl: short}).then(function(result){
        var urlSearch = result[0]["url"]; //www.google.com
        res.redirect(urlSearch) //DOESNT WORK

    });
});

我该怎么做?

推荐答案

尝试使用此代码.它的工作方式类似于charm !!!!!!. 首先,创建一个models文件夹并将其放在shortUrl.js

Try this code.This works like charm!!!!!!. Firsrt create a models folder and place this file in it shortUrl.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Url = new Schema({
    shortUrl:
    {
        type : Number
    },
    url:
    {
        type : String
    }
        });

module.exports = mongoose.model('url', Url);

接下来创建一个routes文件夹,将此文件放在其中urls.js

Next create a routes folder place this file in it urls.js

var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var shortUrl = require('../models/shortUrl');
var app = express();
var url = express.Router();
url.use(bodyParser.json());
url.route('/:short')
.get( function(req, res){
    var short = req.params.short;
     shortUrl.find({shortUrl: short}).then(function(result){

        var urlSearch = result[0]["url"];
        res.redirect('https://'+urlSearch) //DOESNT WORK
});
});
url.route('/num')
.post(function(req,res){
      shortUrl.create(req.body,function(err,url){
if (err) return console.log(error); 
        return res.send(url);
     });

})
    app.use('/url',url);


module.exports = url;

接下来创建一个配置文件,以便您可以进行连接config.js

Next create a config file so that you can give connections config.js

module.exports = {
    'secretKey': '12345-67890-09876-54321',
    'mongoUrl' : 'mongodb://localhost:27017/redirect'
}

现在创建一个服务器文件,例如express.js

And now create a server file like express.js

 var express = require('express');
    var passport = require('passport');
    var LocalStrategy = require('passport-local').Strategy;
    var config = require('./config');
    var mongoose = require('mongoose');
    mongoose.Promise = require('bluebird');
    mongoose.connect(config.mongoUrl);
    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function () {

        console.log("Connected correctly to server");
    });

    var app = express();

    var url = require('./routes/url');

    var shortUrl = require('./models/shortUrl');
    app.use('/url',url);

    app.listen(3000,function(){
    console.log("Server listening on port 3000");
    });

输出:以node express.js

每当您要发布时使用http://localhost:3000/url/num并以json格式提供详细信息.每当您要获取ie时,请使用http://localhost:3000/url/:shrot重定向到任何页面.这对您有帮助.

Whenever you want to post use http://localhost:3000/url/num and give the details in json format.Whenever you want to get i.e.,redirect to aany page use http://localhost:3000/url/:shrot.Here :short nothing but a number should be passed a parameter.Hope this helps for you.

这篇关于如何使用Node.js重定向到Google之类的其他页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆