admin管理员组文章数量:1290316
I am trying to get an input from my main site. After the input is submitted the page should redirect to /view
. It seems like it successfully redirects to /view
(because console.log()
is getting triggered, but res.render
is not working. If i manually go to /view
it is rendering the page.
Here is the code of my app.js
file:
// load the things we need
var express = require('express');
var app = express();
let bodyParser = require("body-parser");
let article = '';
//Set the view engine to ejs
app.set('view engine', 'ejs');
app.use('/static', express.static('static'))
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
//Index page
app.get('/', function (req, res) {
res.render('index')
});
//Get the input from the form
app.post('/searcharticle', function (req, res) {
article = req.body.article;
res.redirect('/view')
return article;
});
//Page to output the input
app.get('/view', function (req, res) {
console.log(article)
res.render('view', {
article: article
})
})
app.listen(8080);
console.log('Server on 8080');
And here is my folder structure
Thank you for your help!
I am trying to get an input from my main site. After the input is submitted the page should redirect to /view
. It seems like it successfully redirects to /view
(because console.log()
is getting triggered, but res.render
is not working. If i manually go to /view
it is rendering the page.
Here is the code of my app.js
file:
// load the things we need
var express = require('express');
var app = express();
let bodyParser = require("body-parser");
let article = '';
//Set the view engine to ejs
app.set('view engine', 'ejs');
app.use('/static', express.static('static'))
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
//Index page
app.get('/', function (req, res) {
res.render('index')
});
//Get the input from the form
app.post('/searcharticle', function (req, res) {
article = req.body.article;
res.redirect('/view')
return article;
});
//Page to output the input
app.get('/view', function (req, res) {
console.log(article)
res.render('view', {
article: article
})
})
app.listen(8080);
console.log('Server on 8080');
And here is my folder structure
Thank you for your help!
Share Improve this question edited Aug 30, 2018 at 8:16 Albert Koholić asked Aug 30, 2018 at 7:56 Albert KoholićAlbert Koholić 411 gold badge1 silver badge4 bronze badges 5- is there any error? – mehta-rohan Commented Aug 30, 2018 at 7:59
- no there are no errors output @mehta-rohan – Albert Koholić Commented Aug 30, 2018 at 8:07
- need to see your folder structure – mehta-rohan Commented Aug 30, 2018 at 8:09
- I added the folder structure to the question @mehta-rohan – Albert Koholić Commented Aug 30, 2018 at 8:17
-
Unrelated: you have a race condition in your code because you store
article
globally. Request specific data does not belong in the global scope. You can for example storereq.body.article
it in the session, file system or a database. Or justres.render
directly in your POST handler (there is also no need to return anything from it as the return value is discarded) – Prinzhorn Commented Aug 30, 2018 at 10:06
6 Answers
Reset to default 4use view engine first then set the directory where to use it.
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
and for static files use
app.use('/static',express.static(PATH.join(__dirname+'/static'));
supposed you have al your .ejs files in views folder try adding this line in your code:
const path = require('path'); //npm install -S path in your console to install path.
//Set the view engine to ejs
app.set('views', path.join(__dirname, 'views')); // add this one, change 'views' for your folder name if needed.
app.set('view engine', 'ejs');
app.use('/static', express.static('static'))
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
If you are running your server from a diff directory than static you need to add a relative path
app.use('/static', express.static(__dirname+'/static'))
I was just given the solution to a very similar problem by my wonderful tutor. The login form 'submit' was passed to jQuery on('click') and after authetication called a Res.Render of the results of an API call. The API call result set was log-abled, but the page never rendered, and there was no error.
He told me that jQuery acts as a 'shadow' DOM, and that the Res.Render was likewise taking place in that shadow DOM. I changed the login form to a form Post instead of the jQuery on Click.
That WORKED!
I worked on this for several days without ever thinking jQuery could cause that obstacle.
You have to mention the layout
app.get('/', function (req, res) {
res.render('index',{layout:false})
});
My way is to go back the version of ejs to 2.5.2, and that works. However, I don't know the reason why it cannot support the version 3..
本文标签: javascriptExpress resrender is not rendering the pageStack Overflow
版权声明:本文标题:javascript - Express res.render is not rendering the page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741498022a2381922.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论