I have loaded root route.
app.get('/', (req, res, next) => {
// loading an HTML page
});
We have loaded an HTML page where we have a submit button, submitting on this submit button, we load the "search" route.
This "/search" route is loading successfully, but we have to load some dynamic content and after fetching all data, we have to load another route like "/searchoutput"
When we call :
app.use('/searchoutput', router);
or
app.get('/searchoutput', router);
But nothing happens without any error.
Thanks
Kumar Anil
So, if I understood correctly, you could do something like this,
If it is a POST request:
app.post("/search", (req, res) => {
// some code
res.redirect("searchoutput");
});
app.get("/searchoutput" (req, res) => {
// some code
res.render("<your page>", { <yourResult>: <yourResult>);
});
If it is a GET request:
app.get("/search", (req, res) => {
// some code
res.redirect("searchoutput");
});
app.get("/searchoutput" (req, res) => {
// some code
res.render("<your page>", { <yourResult>: <yourResult>);
});
If you don't want to reload the page, you could use AJAX, calling onclick a function that receives data from express.
Related
I have a simple express function.
app.post('/thing', ensureLoggedIn("/loginpage"), urlencodedParser, (req, res) => {
var func = req.body.func
res.redirect('/')
somethingElse()
})
I want to return the web page, and then, do the somethingElse.
Surprisingly this just doesn't work. The web browser site there and waits until somethingElse is done, and then, reloads.
If I do this ...
app.post('/thing', ensureLoggedIn("/loginpage"), urlencodedParser, (req, res) => {
var func = req.body.func
res.redirect('/')
setTimeout(longRunningCalculation, 1100) // sloppy but WTF
})
It "works" but obviously is crap.
By "works" I mean, the web page DOES reload instantly; then 1.1 seconds later the long process starts and works as expected.
(Bizarrely if I do a small time, like say "100", it "does not" work; it will, again, behave so that the web page only reloads, once, the long calculation is done.)
What's the solution?
It seems that calling next(); should help you. You can try to do following:
app.post('/thing', ensureLoggedIn("/loginpage"), urlencodedParser, (req, res) => {
var func = req.body.func;
res.redirect('/');
next();
longRunningCalculation();
})
And you can look towards this to do things in more right way:
process.nextTick(() => {
// do something
});
Express has the ability to chain functions via callbacks so u can obviously send out response in one function and actually return in the next like below:
app.post('/thing', ensureLoggedIn("/loginpage"), urlencodedParser, (req, res, next) => {
var func = req.body.func;
res.redirect('/');
next();
},() => {
//implement the long running calculation function
return;
});
I have an index.pug page that is:
head
title My Title
body
form(method="GET", action='/#{redirect_url}')
button(type='submit') run foo
and in my app.js I have:
app.get('/', (req, res) => {
res.render('index', {
redirect_url: '/foo'
});
});
app.get('/foo', (req, res) => {
res.status(200).send('Foo triggered');
});
I am being redirected to http://localhost:3000/?#{redirect_url}.
I want to get redirected to http://localhost:3000/redirect_url?
It works fine when I do not use variables.
I have attached a screenshot for reference.
try
action=`${baseRoute}/${redirect_url}`
I want to redirect a get request to another server post. i don't know how to do that. the following is my code.
server 1: get method
app.get('/sampleTestApp', function (req, res) {
body={uid:'test'}
// Want to redirect here to server 2 'login/callback' url with body object
}
server 2: post method
app.post('/login/callback', function (req, res) {
// success process..
}
You can do something like
app.get('/sampleTestApp', function (req, res) {
axios.post(url:'server2/post', data :body={uid:'test'},function(response) {
res.send(response);
})
}
In my node app, I have routes called index and home. While trying to redirect from index to home route it throws the following error:
Error: Can't set headers after they are sent
Here is the code:
app.get('/index', function (req, res) {
res.redirect('/home');
})
app.get('/home', function (req, res) {
res.send('Welcome Home !!');
})
You can try replace '/index' with "/index". It works fine after that.
I have the following code :
app.get('/payment', function(req, res) {
// do lots of stuff
});
now I want to add the following :
app.post('/payment', function(req, res) {
req.myvar = 'put something here';
// now do the same as app.get() above
});
Obviously I want to reuse the code. I tried doing next('/payment') inside the post handler and put it above the get handler, but no luck, probably because they are different VERBs.
What are my options ?
Thanks.
Just lift out the middleware to its own function and use it in both routes.
function doLotsOfStuff (req, res) {
// do lots of stuff
}
app.get('/payment', doLotsOfStuff);
app.post('/payment', function(req, res, next) {
req.myvar = 'put something here';
next();
}, doLotsOfStuff);