我正在构建一个非常小的示例应用程序来练习登录页面。
我正在与:
1)Bootstrap
2)Bootswatch-特别是日记形式
3)Font Awesome
成功设置所有组件后,如果我在负责渲染localhost:5000窗口的index.js
文件中应用以下更改,则会遇到一个非常奇怪的错误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/[email protected]/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>
错误:
错误:无法在视图目录中查找视图“布局”
“ / home / emanuele / Desktop / cashman-tracker-dashboard / views”
在Function.render(/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/application.js:580:17)
在ServerResponse.render(/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/response.js:1012:7)
在/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express-ejs-layouts/lib/express-layouts.js:113:20
在tryHandleCache(/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/ejs/lib/ejs.js:260:5)
在View.exports.renderFile [作为引擎](/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/ejs/lib/ejs.js:459:10)
在View.render(/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/view.js:135:8)
在tryRender(/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/application.js:640:10)
在Function.render(/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/application.js:592:3)
在ServerResponse.render(/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/response.js:1012:7)
在ServerResponse.res.render(/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express-ejs-layouts/lib/express-layouts.js:77:18)
到目前为止,我所做的是:
1)我认为这是由于
render
方法引起的错误,并检查了official documentation about it,但是我编写的一小段代码没有发现任何问题。2)如果更易于理解,还提供了打印屏幕:
请指出解决此问题的正确方向
最佳答案
查找目录中没有名称为view
的welcome
。
使用res.send('welcome')
时,快递将仅发送welcome
text
发送响应,因此不会查找任何view
页面。
但是另一方面,当您使用res.render
时,它将搜索名称为template
的welcome.ejs
视图。
res.render用于加载ejs视图文件
用welcome.ejs
在视图中创建文件,然后尝试。
希望这能解决问题。
关于javascript - React JS-表达:res.send VS res.render错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60079814/