ReferenceError: index is not defined (Node.js, Express.js)
ReferenceError: index is not defined (Node.js, Express.js)
我基本上是在尝试使用 express 和 hbs 来动态呈现页面。我还包含了必要的 npm 包。我还在视图目录中创建了 index.hbs,但是当我尝试 运行 我的代码时,出现以下错误。
ReferenceError: index is not defined
at C:\Users\Moaz\Desktop\Node JS Code\web-server\src\app.js:11:14
at Layer.handle [as handle_request] (C:\Users\Moaz\Desktop\Node JS Code\web-server\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Moaz\Desktop\Node JS Code\web-server\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Moaz\Desktop\Node JS Code\web-server\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Moaz\Desktop\Node JS Code\web-server\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Moaz\Desktop\Node JS Code\web-server\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Moaz\Desktop\Node JS Code\web-server\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Moaz\Desktop\Node JS Code\web-server\node_modules\express\lib\router\index.js:275:10)
at SendStream.error (C:\Users\Moaz\Desktop\Node JS Code\web-server\node_modules\serve-static\index.js:121:7)
at SendStream.emit (node:events:376:20)
app.js代码:-
const path = require("path");
const express = require("express");
const app = express();
const publicDirectoryPath = path.join(__dirname, "../public");
app.set("view engine", "hbs");
app.use(express.static(publicDirectoryPath));
app.get("", (req, res) => {
res.render(index);
});
app.listen(3000, () => {
console.log("Service is up and running successfully!");
});
VS Code目录截图:-
你把index
写成一个变量。
将其设置为字符串:
app.get("*", (req, res) => {
res.render('index');
});
您正在渲染索引页面错误,您需要将字符串传递给渲染函数,请在第 11 行尝试 res.render('index');
。
其次,我错了,将 views 文件夹放在 src 文件夹中,运行 应用程序。或者根据 web-server/views/
调整你的路径
我基本上是在尝试使用 express 和 hbs 来动态呈现页面。我还包含了必要的 npm 包。我还在视图目录中创建了 index.hbs,但是当我尝试 运行 我的代码时,出现以下错误。
ReferenceError: index is not defined
at C:\Users\Moaz\Desktop\Node JS Code\web-server\src\app.js:11:14
at Layer.handle [as handle_request] (C:\Users\Moaz\Desktop\Node JS Code\web-server\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Moaz\Desktop\Node JS Code\web-server\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Moaz\Desktop\Node JS Code\web-server\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Moaz\Desktop\Node JS Code\web-server\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Moaz\Desktop\Node JS Code\web-server\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Moaz\Desktop\Node JS Code\web-server\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Moaz\Desktop\Node JS Code\web-server\node_modules\express\lib\router\index.js:275:10)
at SendStream.error (C:\Users\Moaz\Desktop\Node JS Code\web-server\node_modules\serve-static\index.js:121:7)
at SendStream.emit (node:events:376:20)
app.js代码:-
const path = require("path");
const express = require("express");
const app = express();
const publicDirectoryPath = path.join(__dirname, "../public");
app.set("view engine", "hbs");
app.use(express.static(publicDirectoryPath));
app.get("", (req, res) => {
res.render(index);
});
app.listen(3000, () => {
console.log("Service is up and running successfully!");
});
VS Code目录截图:-
你把index
写成一个变量。
将其设置为字符串:
app.get("*", (req, res) => {
res.render('index');
});
您正在渲染索引页面错误,您需要将字符串传递给渲染函数,请在第 11 行尝试 res.render('index');
。
其次,我错了,将 views 文件夹放在 src 文件夹中,运行 应用程序。或者根据 web-server/views/
调整你的路径