javascript - Express res.render 没有渲染页面

标签 javascript node.js express ejs

我正在尝试从我的主站点获取输入。提交输入后,页面应重定向到 /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/

相关文章:

javascript - 如何在没有 ng-model 的情况下以编程方式更改 angular 2 形式的原始属性

javascript - 如何开始使用第一版 javascript sdk

node.js - 如何从 AWS DynamoDB 中的函数返回查询结果 - JS lambda

node.js - 快速文件上传。处理 Formidable Parse 中的错误(服务器端)

javascript - 从 Express 中调用内部 API

JavaScript:如何在字符串中设置变量

javascript - 在 d3 中鼠标悬停时更新行顺序(z 方向)

node.js - 如何正确处理 nodejs 中的 SequelizeConnectionError

node.js - 如何知道 reCAPTCHA v3 是否有效?

node.js - 在 AWS 负载均衡器后面运行时如何强制 node.js/express.js 使用 HTTPS