Node.JS and express

编程入门 行业动态 更新时间:2024-10-10 23:27:05

<a href=https://www.elefans.com/category/jswz/34/1771450.html style=Node.JS and express"/>

Node.JS and express

我的POST功能有问题。当我按下登录按钮时,出现以下消息:

无法发布/登录

我不知道该怎么办。我希望你们中的一个可以帮助我\(^-^ \)

这是我的代码:

    const express = require('express')
    const app = express()
    const { check } = require('express-validator')
    const http = require('http')
    const port = 3000

    app.use(express.static('./public', {index: 'login.html'}))
    app.use(express.urlencoded());

    // Routing
    let loginRouter = express.Router()

    app.use('/login', loginRouter)

    loginRouter.all('/login', function (req, res) {
        console.log('POST')
        console.log(req.body)
        console.log('GET')
        console.log(req.query)
    })
    app.listen(port, function() {
        console.log('listen on http://localhost:' + port)
    })
<script src=".6.3/umd/react.production.min.js"></script>
<script src=".6.3/umd/react-dom.production.min.js"></script>
<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Login 1</title>
        <link rel="stylesheet" href="login.css">
    </head>
    <body>
        <form method="post" action="/login">
            <div>
                <label for="user">Username:</label>
                <input type="text" id="user" />
            </div>
            <div>
                <label for="userpw">Password:</label>
                <input type="password" id="userpw" />
            </div>
            <div>
                <button type="submit">Log in</button>
            </div>
        </form>
    </body>
</html>
回答如下:

您使用以下方法在路径loginRouter中将/login注册为中间件:

app.use('/login', loginRouter)

这意味着您在路由器中侦听的任何路径都将成为/login的子路径。

因此,您的实际链接将变为/login/login

您可能想要删除上述中间件并重试。

更多推荐

Node.JS and express

本文发布于:2024-05-07 12:16:55,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Node   JS   express

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!