Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express res.render is not rendering the page

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!

like image 539
Albert Koholić Avatar asked Oct 18 '25 13:10

Albert Koholić


1 Answers

use 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'));
like image 61
sachin Avatar answered Oct 21 '25 03:10

sachin