我正在尝试从我的主站点获取输入。提交输入后,页面应重定向到/view
。看起来它成功重定向到/view
(因为console.log()
正在被触发,但是res.render
不管用。如果我手动去/view
它正在渲染页面。
这是我的代码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');
这是我的文件夹结构 https://i.stack.imgur.com/Wwlke.png
感谢您的帮助!
首先使用视图引擎,然后设置使用它的目录。
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
和静态文件使用
app.use('/static',express.static(PATH.join(__dirname+'/static'));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)