node.js - express res.redirect target page js not loaded -
i using node.js + express + jade demo simple pages. got problem 2 days. googled lot, cannot find answer. basically, redirect page another. , on target page, have socket.io code inside document.ready. problem /pageone, pagetwo rendered correctly(but url in browser still /pageone), code inside document.ready not executed.
my router.js
app.post('/pageone', session, function(req, res){ res.redirect('/pagetwo'); }); app.get('/pagetwo', function(req, res){ res.render('pagetwo', { title: 'demo' }); });
my pagetwo jade
doctype html head title #{title} - site link(rel='stylesheet', href='/css/style.css') script(type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js') script(type='text/javascript' src='https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js') script. $(document).ready(function() { alert("i alert box!"); }); body h1 demo
the result can see demo on page alert box not showing. if directly visit /pagetwo, alert box showing.
many thanks
////// edited ////// app.js
var express = require('express') , http = require('http') , session = require('express-session'); var app = express(); var port = process.env.port || 8080; app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.set('port', port); app.use(express.static(__dirname + '/public')); app.use(session({ secret: 'gdfgsdrgesrgerge', cookie: { maxage: 60000 }})) require('./controller/router')(app); var server = http.createserver(app).listen(app.get('port'), function(){ console.log("express server listening on port " + app.get('port')); })
and router.js
var express = require('express'); module.exports = function(app) { app.get('/pageone', function(req, res){ res.render('pageone', { title: 'welcome' }); }); app.post('/pageone', function(req, res){ res.redirect('/pagetwo'); }); app.get('/pagetwo', function(req, res){ res.render('pagetwo', { title: 'demo' }); }); };
Comments
Post a Comment