node.js express - 无法在post route中看到post form的数据。

问题描述 投票:0回答:1

嘿,我想在我的项目中用node做一些基本的登录功能。但我在使用Router时遇到了一些麻烦。我似乎不能从html post form中在终端显示任何信息,出于某种原因,它只能通过POSTMAN工作,我也不能在Auth.js中添加任何get route,然后当我试图访问它时,我得到以下错误。

错误:ENOENT: no such file or or the name of the auth.js: ENOENT: no such file or directory, stat 'xxxxroute'.

如你所见,我已经把登录获取路径移到了app.js中,工作没有问题。

因此,它归结为两件事。

  1. 为什么我不能从login.html获取我的post form数据 然后用auth.js显示出来?
  2. 为什么我不能在Auth.js中添加登录获取路径?

这是app.js

const express = require('express');
const app = express();

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


app.get("/login", (req, res) => {

    return res.sendFile(__dirname + "/public/login.html");
});

const session = require('express-session');
const config = require('./config/config.json');
app.use(session({
    secret: config.sessionSecret,
    resave: false,
    saveUninitialized: true
}));

const rateLimit = require('express-rate-limit');

const limiter = rateLimit({
    windowMs: 15 * 60 * 1000, // 15 minutes
    max: 100 // limit each IP to 100 requests per windowMs
});

app.use(limiter);

const authLimiter = rateLimit({
    windowMs: 15 * 60 * 1000, // 15 minutes
    max: 8 // limit each IP to 8 requests per windowMs
});

app.use('/signup', authLimiter);
app.use('/login', authLimiter);


/* Setup Knex with Objection */
const { Model } = require('objection');
const Knex = require('knex');
const knexfile = require('./knexfile.js');
const knex = Knex(knexfile.development);

Model.knex(knex);

const authRoute = require('./routes/auth.js');
const usersRoute = require('./routes/users.js');

app.use(authRoute);
app.use(usersRoute);

const PORT = 3000;

app.listen(PORT, (error) => {
    if (error) {
        console.log(error);
    }
    console.log("Server is running on the port", PORT);
})


这是我的 auth.js

const router = require('express').Router();

router.post('/login', (req, res) => {

    console.log(req.body)
    let userlogin = {
        username: req.body.uname,
        password: req.body.psw
      }

    console.log(userlogin);

    res.send({ response: userlogin });
});

module.exports = router;


这是公共文件夹下的login.html。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body{
            text-align: center;
            margin-top: 15%;
        }
        .container{
            padding: 5%;
            margin: 2%;
        }
    </style>
    <title>login</title>
</head>
<body>
    <h1>Login site</h1>
    <form  action="/login" method="POST">
        <div class="container">
          <label for="uname"><b>Username</b></label><br>
          <input type="text" placeholder="Enter Username" name="uname" ><br>

          <label for="psw"><b>Password</b></label><br>
          <input type="password" placeholder="Enter Password" name="psw" ><br><br>

          <button type="submit">Login</button><br><br>
        </div>
    </form> 

    <div>
        <form action="/signup.html">
            <input type="submit" value="signup" />
        </form>
    </div>
</body>
</html>
html node.js forms express post
1个回答
0
投票

程序提示您使用的是位于公共文件夹中的静态页面。相反,你需要使用模板来使用登录凭证。

去伪存真,EJS。

© www.soinside.com 2019 - 2024. All rights reserved.