Express with EJS/ETA redirect errors(302)?

Viewed 26

I've been using EJS with Express in my app to do some basic SSR(show login button or profile button depending on login status and similar things). It worked fine but when I did some benchmarkes it turned out it was severely limiting the rate of requests per second of the server. This is why I decided I would replace it with ETA. It is supposed to have almost identical syntax and be substantially quicker at doing the same thing.

After I did that I encountered a strange 302("The page isn’t redirecting properly") redirect error in the browser: enter image description here

I have checked NodeJs but it doesn't throw any errors or warnings. It starts up and runs normally. I tried undoing the changes, reinstalling EJS and renaming the templates to EJS again but I still get the issue. It doesn't want to render even if it just contains pure HTML so I don't think it's a problem with the .eta files.

(Req.user returns if user is logged in (from authController.isLoggedIn))

// App.js (server init)

    const express = require('express');
    const app = express();
    require('dotenv').config();
    const port = process.env.port;
    const expressValidator = require('express-validator');
    const eta = require("eta");
    
    // Parsing
    app.use(express.json({ limit: '10kb'}));
    app.use(express.urlencoded({ extended: false }));
    app.use(expressValidator());
    
    // Set views
    app.engine("eta", eta.renderFile); //doesn't work regardless if it's on or off
    app.set('views', './sites/');
    app.set("view engine", "eta");
    app.set('trust proxy', 1);
    
    // Define routes
    app.use('/', require('./routes/sites'));
    app.use('/auth', require('./routes/auth'));
    app.use('/', express.static('./public', { maxAge: 31557600, }));
    app.use('/act/:topic/:id', express.static('./public', { maxAge: 31557600 }));
   
    
    // Error handler (for now)
    app.use((req, res, next) => {
        const err = new Error("Not found");
        err.status = 404;
        next(err);
    });
    app.use((err, req, res, next) => {
        if(err.status == 404) {
            res.status(err.status);
            req.flash("flash", '404 page not found.');
            return res.redirect('back');
        } else {
            res.status(500);
            req.flash("flash", 'A server error has occurred.');
            return res.redirect('back');
        }
    });
    
    // Listen on 3306
    app.listen(port, process.env.host, () => console.log(`Server started on port ${port}.`));

// Sites.js (routes)

const express = require('express');
const router = express.Router();
const authController = require('../controllers/auth');
const dotenv = require('dotenv');
dotenv.config({ path: './.env'});
router.use(express.json());

router.get('/robots.txt', (req, res) => {
  res.header('Content-Type', 'text/html');
  res.render('robots')
});
router.get('/about', authController.isLoggedIn, (req, res) => {
  res.render('about', {
    user: req.user, message: 'About us'
  });
});

// I've also tried this
router.get('/', authController.isLoggedIn, (req, res, next) => {
  var aaa = eta.renderFile('index', {
    user: req.user, message: 'Enjoy your visit'
  });
  res.send(aaa);
});


module.exports = router;

// About.eta (I have also tried if it would work without layout or with include but it didn't)

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Scrooc: About us</title>

        <% layout("./modules/link") %>
    </head>
    <body >

        <% layout("./modules/head") %>

        <main>
            <!-- just HTML -->
        </main>
    </body>
</html> 
0 Answers
Related