使用Express无法呈现HTML页面

我正在尝试制作一个简单的应用程序,在其中我可以在快速应用程序中的名称列表中添加名称。但是,每当我提交一个新用户时,users.html页面都不会加载,并且出现以下错误:path must be absolute or specify root to res.sendFile

我已经尝试查看网站上的express.static(public)文档,但无济于事。

这是文件

index.html

  <form action="users" method="post">
        Name:
        <br>
        <input type="text" name='name'>
        <button type="submit" value="submit">Add User</button>
    </form>

users.html

    <ul id='users'>
        // will append users here
    </ul>

server.js

const express = require("express");
const app = express();
const PORT = process.env.PORT || 1000;

let users = ["John","Danny","Ashley"]

app.use(express.urlencoded({ extended: true }))
app.use(express.json({}))

app.use(express.static("public"))

// Root route,render index.html page
app.get("/",(req,res) => {
    res.sendFile("index.html")
})

app.get("/users",res) => {
    res.json(users)
})

app.post("/users",res) => {
    users.push(req.body.name)
    res.sendFile(__dirname + '/users.html')
})

app.listen(PORT,() => {
    console.log(`Server is listening on PORT: ${PORT}`)
})
qhslz 回答:使用Express无法呈现HTML页面

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3155888.html

大家都在问