尝试使用Node Express应用发送请求时发生超时错误

编程入门 行业动态 更新时间:2024-10-09 14:21:47

尝试使用Node Express应用发送请求时发生超时<a href=https://www.elefans.com/category/jswz/34/1771449.html style=错误"/>

尝试使用Node Express应用发送请求时发生超时错误

我正在学习一个教程,以创建一个将数据发送到本地服务器的表单,但出现超时错误。这是我第一次使用node.js express应用程序处理帖子请求,但我陷入了困境。

这是车把代码:

form method="POST" action="send">
  <div class="form-row">
    <div class="col-md-4 mb-3">
      <label for="validationServer01">First name</label>
      <input type="text" class="form-control is-valid" id="validationServer01" placeholder="First name" value="Mark" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>
    <div class="col-md-4 mb-3">
      <label for="validationServer02">Last name</label>
      <input type="text" class="form-control is-valid" id="validationServer02" placeholder="Last name" value="Otto" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>
    <div class="col-md-4 mb-3">
      <label for="validationServerUsername">Username</label>
      <div class="input-group">
        <div class="input-group-prepend">
          <span class="input-group-text" id="inputGroupPrepend3">@</span>
        </div>
        <input type="text" class="form-control is-invalid" id="validationServerUsername" placeholder="Username" aria-describedby="inputGroupPrepend3" required>
        <div class="invalid-feedback">
          Please choose a username.
        </div>
      </div>
    </div>
  </div>
  <div class="form-row">
    <div class="col-md-6 mb-3">
      <label for="validationServer03">City</label>
      <input type="text" class="form-control is-invalid" id="validationServer03" placeholder="City" required>
      <div class="invalid-feedback">
        Please provide a valid city.
      </div>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationServer04">State</label>
      <input type="text" class="form-control is-invalid" id="validationServer04" placeholder="State" required>
      <div class="invalid-feedback">
        Please provide a valid state.
      </div>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationServer05">Zip</label>
      <input type="text" class="form-control is-invalid" id="validationServer05" placeholder="Zip" required>
      <div class="invalid-feedback">
        Please provide a valid zip.
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="form-check">
      <input class="form-check-input is-invalid" type="checkbox" value="" id="invalidCheck3" required>
      <label class="form-check-label" for="invalidCheck3">
        Agree to terms and conditions
      </label>
      <div class="invalid-feedback">
        You must agree before submitting.
      </div>
    </div>
  </div>
  <button class="btn btn-primary" type="submit">Submit form</button>
</form>

这是入口代码:

onst express = require ('express');
const bodyParser = require ('body-parser');
const exphbs = require ('express-handlebars');
const nodemailer = require ('nodemailer');
const path = require ('path');

const app = express ();


//View Engine Setup
app.engine ('handlebars', exphbs());
app.set ('view engine', 'handlebars');

//Static Folder
app.use('/public', express.static(path.join(__dirname, 'public')));

//Body Parser Middleware

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());


//route

app.get ('/', (req,res) => {
    res.render('contact', {layout:false})
});

//post route for submission
app.post('/send', (req, res) => {
    console.log(req.body);
});

app.listen (3000,() => console.log ('Server started...'));

我在服务器上得到的全部是:{}当我尝试提交时。

回答如下:

[每当您向服务器发送表单值时,请始终在输入字段中添加name属性。否则,您将无法在服务器端获得请求参数(req.body)。

name属性在您的情况下用作JSON属性。如果您不提供name属性,则JSON不会使用字段值进行更新。

更多推荐

尝试使用Node Express应用发送请求时发生超时错误

本文发布于:2024-05-06 19:05:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1753693.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   发生   Node   Express

发布评论

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

>www.elefans.com

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