Kaya Toast Kaya Toast - 15 days ago 9
Node.js Question

Express res.render triggered by app.post

Within the stub created by express application generator, I've created a new view called

test
.

I am unable to invoke
test
using:

app.post('/test', function(req, res, next) {
res.render('test');
});


It seems surprising, because
app.get
below is getting rendered as expected:

app.get('/test', function(req, res, next) {
res.render('test');
});


I have further tried the following, but it is still not rendering the page:

app.post('/test', function(req, res, next) {
console.log('within app.post'); // this is getting logged
res.redirect('/test');
});


Please help. How can I render
test
from a post method ?

Answer

So probably there is problem with javascript code. I am add code below to file index.js in routes folder, and template is rendering:

router.get('/test', function(req, res, next) {
    res.render('index', {title: 'GET test'});
});

router.post('/test', function(req, res, next) {
    res.render('index', {title: 'POST test'});
});

Try to send request via curl:

curl -v -XPOST http://localhost:3000/test

Or use chrome or firefox extension to make request.