【问题标题】:Form handling in Node.jsNode.js 中的表单处理
【发布时间】:2016-03-10 20:43:07
【问题描述】:

到目前为止一直在学习 Node.js,构建了一些 MVC 应用程序,但仍然无法正确处理表单,我需要一些帮助来弄清楚事情是如何工作的。

好的,弄清楚这个东西是如何工作的最好方法是关于我之前的问题,Steam Web API 编程。

我安装了 express 和 steamwebapi。我的 index.html 看起来像这样:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Steam</title>
</head>
<body>
<form action="/ask_user_id" method="POST">
    <input type="text" name="id"><br>
    <input type="submit" value="Pošalji">
</form>
</body>
</html>

我的app.js

    //Express
var express = require('express');
var server = express();

//Steam
var SteamWebAPI = require('steamwebapi').SteamWebAPI;
SteamWebAPI.setAPIKey('xxx key is here ');

//Rendering form in index.html file
server.get('/ask_user_id', function(req, res) {
    app.render('form', function(err, html) {
        if(err)
            return res.send(err);
        else
            return res.send(html)
    });
});

//Route for handling form submission
server.post('/user_infos', function(req, res) {
    //Form submitted in req.body, retriving userID

    var _userID = req.body.userId;

    //SteamWebAPI - getRecentlyPlayedGames
    SteamWebAPI.getRecentlyPlayedGames(_userID, 5, function(response) {
        return res.json(response.response.games);
    });
});

//Localhost
server.listen(3000, function() {
    console.log('Server: 3000');
});

我正在使用 IntelliJ IDEA。我输入终端节点 App.js,服务器:3000。如果我转到我的 index.html 文件,它会将我重定向到 http://localhost:63342/Steam/index.html,如果我在表单中输入内容,它会将我重定向到:http://localhost:63342/ask_user_id,然后我得到“404 Not Found”。

我做错了什么?如果我输入 node app.js 然后转到 localhost:3000/ask_user_id 我得到参考错误。曾经有人问我为什么需要应用程序;不知道,如果我放server又会报错。

【问题讨论】:

  • 无意冒犯,但如果您不知道自己在做什么,则应该阅读众多在线 Node.js 教程之一。然后,一旦它工作,就将 Steam 放入其中。
  • 当然,我给自己买了一本书,用 Node.js 和 Express 进行 Web 开发,但是如果我不练习,我怎么能学呢?这几周我学到了很多,但这是必不可少的,显然没有什么太难……或者我错了
  • 你有本书真好。我的意思是你可以找到许多教程(我相信你的书中有一个部分),这些教程介绍了如何设置表单。不过,在这一点上,通过集成 Steam API 来增加复杂性是没有意义的。
  • 我可以处理表单,但不能用 Steam api 处理...
  • 在你的html中,改为action="/user_infos"

标签: javascript node.js forms intellij-idea


【解决方案1】:

在您的 html 中,更改为 action="/user_infos"

请求会这样进行

GET /ask_user_id   <- this is you going to localhost:3000/ask_user_id in browser
POST /user_infos   <- this is you submitting the data to the URL specified in the action attribute of your form.

【讨论】:

  • 现在我收到错误:ReferenceError: app is not defined... :(
  • 用您正在运行的当前代码更新您的问题
  • app.js 的第 11 行,您致电 app.render()。它应该是server.render(),因为这就是你在第 3 行所说的快递应用程序:var server = express();
  • 嗯,我正在这样做,但我得到了奇怪的错误:错误:没有指定默认引擎并且没有提供扩展名。?这是什么意思?
  • so express 通常与模板引擎一起使用,而不仅仅是直接的 html。谷歌 EJS。它可以让您将数据粘贴到您的 html 中,但它也只会让您输入常规 html
【解决方案2】:

客户端中的 post action 是“ask_user_id”

更改服务器以监听

server.post('/ask_user_id',

没有 server.get

【讨论】:

  • 如果我在这种情况下在表单中输入我的 Steam ID:'76561198190043289',我再次得到错误:404 Not Found,url 仍然是:localhost:63342/ask_user_id,但如果我去:localhost: 3000/ask_user_id,我得到:无法获取 /ask_user_id
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-07
  • 1970-01-01
  • 2016-10-10
  • 2018-06-08
  • 2014-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多