Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138192

React js - express: res.send Vs res.render Error

$
0
0

I am building a very small example application to practice a login page. I am working with :

1) Bootstrap

2) Bootswatch - in particular with Journal form

3) Font Awesome

After successfully setting up all my components I have a very strange error if I apply the following change in my index.js file in charge of rendering my localhost:5000 window

router.get('/', (req, res) => res.send('welcome')); <-- This way works

router.get('/', (req, res) => res.render('welcome')); <-- This way throws error

app.js

const express = require('express');
const expressLayouts = require('express-ejs-layouts');

const app = express();
app.use(expressLayouts);
app.set('view engine', 'ejs');

// Routes
app.use('/', require('./routes/index'));
app.use('/users', require('./routes/users'));

const PORT = process.env.PORT || 5000;
app.listen(PORT, console.log(`Server started on port ${PORT}`));

index.js

const express = require('express');
const router = express.Router();
router.get('/', (req, res) => res.send('welcome')); // <-- This way works
router.get('/', (req, res) => res.render('welcome')); // <-- This way throws error
module.exports = router;

users.js

const express = require('express');
const router = express.Router();

// Login Page
router.get('/login', (req, res) => res.send('Login'));
// Register Page
router.get('/register', (req, res) => res.send('Register'));
module.exports = router;

layouts.ejs

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://kit.fontawesome.com/My_KIT.js" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="../components/bootstrap.min.css" />
    <title>App</title>
</head>
<body>
    <div class="container">
        <%- body %>
    </div>
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

</body>
</html>

The error:

Error: Failed to lookup view "layout" in views directory "/home/emanuele/Desktop/cashman-tracker-dashboard/views" at Function.render (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/application.js:580:17) at ServerResponse.render (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/response.js:1012:7) at /home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express-ejs-layouts/lib/express-layouts.js:113:20 at tryHandleCache (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/ejs/lib/ejs.js:260:5) at View.exports.renderFile [as engine] (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/ejs/lib/ejs.js:459:10) at View.render (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/view.js:135:8) at tryRender (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/application.js:640:10) at Function.render (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/application.js:592:3) at ServerResponse.render (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/response.js:1012:7) at ServerResponse.res.render (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express-ejs-layouts/lib/express-layouts.js:77:18)

What I have done so far:

1) I thought it was an error due to the render method and went to check the official documentation about it but I didn't see any problem with the small portion of code I wrote.

2) A print screen is also provided if that is easier to understand:

print

Please point to the right direction for solving this issue


Viewing all articles
Browse latest Browse all 138192

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>