我正在尝试从我的主站点获取输入。提交输入后,页面应重定向到 /view
。它似乎成功重定向到 /view
(因为 console.log()
被触发,但 res.render
不工作。如果我手动转到 /view
它正在呈现页面。
这是我的 app.js
文件的代码:
// 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');
这是我的 folder structure
感谢您的帮助!
最佳答案
首先使用 View 引擎,然后设置使用它的目录。
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
静态文件使用
app.use('/static',express.static(PATH.join(__dirname+'/static'));
关于javascript - Express res.render 没有渲染页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52091903/