I'm trying to do a simple a 301 redirection. My code for that looks something like this:
app.get('/',function(req,res){
console.log(req.get('host')+req.originalUrl);
if(req.get('host')+req.originalUrl == "facebook.com:3000/"){
res.redirect(301,'https://youtube.com');
} else {
res.redirect(301,'https://google.com');
}
});
So I what did was edited my host file so that facebook.com was linked to localhost (127.0.0.1)
Problem is my app.get()
isn't being called except for a couple of times( usually when I don't look at app.js file for a while then use it again). After those times, it will always redirect to the same website even if I edit the file for it redirect to other websites. Here is some relevant code in addition to this:
app.locals.cacheBuster = Date.now();
app.set('port', process.env.PORT || argv.p || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.compress());
app.use(express.favicon(path.join(__dirname, 'public/img/favicon.ico')));
app.use(express.logger('dev'));
app.use(express.cookieParser());
app.use(express.json());
app.use(express.urlencoded());
app.use(expressValidator());
app.use(express.methodOverride());
app.use(express.session({
secret: 'your secret code',
store: new MongoStore({
db: mongoose.connection.db,
auto_reconnect: true
})
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res, next) {
res.locals({
user: req.user,
cookies: req.cookies,
pullup: { // global client-side JS object
baseUrl: req.protocol + '://' + req.get('host')
}
});
if (!_.isObject(req.session.flash) || !Object.keys(req.session.flash).length) {
req.session.windowscrolly = 0;
}
if (req.body.windowscrolly) req.session.windowscrolly = req.body.windowscrolly;
res.locals.windowscrolly = req.session.windowscrolly;
res.setHeader("Content-Security-Policy", "script-src 'self' https://apis.google.com http://sysinct.herokuapp.com; frame-src 'self' https://gitter.im;");
res.setHeader("X-Frame-Options", "DENY");
next();
});
app.use(flash());
app.use(less({ src: __dirname + '/public', compress: true }));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public'), { maxAge: week }));
app.use(function(req, res) {
res.render('404', { status: 404 });
});
app.use(function(err, req, res, next){
console.error("req: "+req +"\nerror:"+err.stack);
res.statusCode = 500;
res.render('error',{error:err});
});
app.locals.timeago = timeago;
...eventually,
app.listen(app.get('port'), function() {
console.log("✔ Express server listening on port %d in %s mode", app.get('port'), app.settings.env);
});
I've tried inserting my app.get function into various places around this chunk of code (I'm aware I don't put app.get()
in this example right now because I don't know where to put it) because I read that the order that it has with app.use(app.router)
and other middleware matters, but I've had no luck finding out why the app.get()
isn't called.
app.get()
isn't being calledAre you setting at the top:
var express = require('express');
var app = express();
?
I know before used to work something like
var bodyParser = require('express.bodyParser()');
but no longer, now is separated from express and I am seeing you have a few modules like that, couldnt be that?
For 301 redirects, the chrome browser stores information of the 301 redirect in the cache(so that the server can load data faster in the future). So my app.get wasn't being called because the cache was serving a localized version of the web page instead of requesting from the server again. To make it work, clear the cache in the setting (where browsing history is). Then, it will redirect by requesting to web server accordingly and app.get() will be called.