enter code here
//这是我的app.js代码,我想做一个基本的路由导航栏,这里的demo是我的文件名。我想在演示页面中插入一些HTML,但在浏览器屏幕上什么都没有
const path = require("path");
const app = express();
const port = 80;
// For serving static files
app.use("/static", express.static("static"));
// Set the template engine as pug
app.set("view engine", "pug");
// Set the views directory
app.set("views", path.join(__dirname, "views"));
// Our pug demo endpoint
app.get("/demo", (req, res) => {
res
.status(200)
.render("demo", {
title: "Hey Harry",
message: "Hello there and thanks for telling me how to use pubG!",
});
});
app.get("/", (req, res) => {
res.status(200).send("This is homepage of my first express app with Harry");
});
app.get("/about", (req, res) => {
res.send("This is about page of my first express app with Harry");
});
app.post("/about", (req, res) => {
res.send(
"This is a post request about page of my first express app with Harry"
);
});
app.get("/this", (req, res) => {
res.status(404).send("This page is not found on my website cwh");
});
app.listen(port, () => {
console.log(`The application started successfully on port ${port}`);
});```
enter code here
//& Here is my code for demo.pug
```html
head
title=title
body
h1 = message```
-
请删除文件末尾的反勾号,
-
请在您的文件中包括这一行:
const express = require("express");
-
您可以使用EJS而不是pug。它更受欢迎,更易于书写和理解。
-
假设根目录中有
views
文件夹,则可以删除以下行:
// Set the views directory
app.set("views", path.join(__dirname, "views"));
- 再添加一个
res
将使您的代码看起来更时尚,也不那么bug(例如,消息行的末尾不应该有逗号(:
app.get("/demo", (req, res) => {
res.status(200);
res.render("demo", {
title: "Hey Harry",
message: "Hello there and thanks for telling me how to use pubG!"
});
});
我在电脑上检查了你的代码,它起作用了。请尝试以上步骤,并让我知道它是否有效。