Jeff Goes
Jeff Goes

Reputation: 309

How do I redirect to another page like google using Nodejs?

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.

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

    });
});

How can I do this?

Upvotes: 1

Views: 1554

Answers (2)

Syed Ayesha Bebe
Syed Ayesha Bebe

Reputation: 1448

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);

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;

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'
}

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");
    });

Output : Run the code as node express.js

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.

Upvotes: 1

Bharathvaj Ganesan
Bharathvaj Ganesan

Reputation: 3204

Try this solution

res.writeHead(301,
  {Location: 'http://whateverhostthiswillbe.com'}
);
res.end();

Upvotes: 0

Related Questions