如何将表单从HTML发布到Node.js文件中(How to post a form from HTML into Node.js file)

编程入门 行业动态 更新时间:2024-10-26 00:32:08
如何将表单从HTML发布到Node.js文件中(How to post a form from HTML into Node.js file)

我有一个名为index.html的HTML文件,以及一个名为server.js和logic.js的Node.js文件。

server.js负责创建服务器并加载index.html文件。 HTML文件显示要求执行任何操作的表单(例如:用户名),然后将此数据发布到logic.js文件,该文件将打印此用户名。

怎么可能呢? 谢谢!

I've a HTML file named index.html, and a Node.js files named server.js and logic.js.

server.js is reponsible for creating the server and loading index.html file. The HTML file displays a form asking for any action(ex: username), then it posts this data to the logic.js file which prints this user name.

how could it be done? Thanks!

最满意答案

好吧,没有看到你的任何代码或知道你正在使用什么框架,它将很难具体,但你会想要

在HTTP GET上呈现包含表单的一些HTML 这是最简单的部分。 只需用一些标签写一个简单的文档。 我假设您已经有一些Web服务器来执行此操作。 听取POST请求。 这与GET不同,您的服务器可能具有不同的功能来处理该请求(因为逻辑不同)。 处理POST请求 在这里,您只想从POST数据中提取名称并打印它(或做任何你想做的事情)。

我建议使用expressjs ,因为您可能不想编写处理Web服务器的所有代码。 有了这个框架,渲染HTMl就像这样简单

app.get("/", function(request, response) { res.send(some_html); });

并处理POST将如此简单

app.post("/endpoint", function(request, response) { console.log(request.query.name); });

编辑:为了回答你的问题,在整个单独的文件中分散处理多个页面的逻辑并不难。 我通常做的是拥有一个设置所有内容的server.js,然后将包含处理其他页面逻辑的其他文件。

例如,如果您的应用可能布局如此

server.js /modules/ /modules/index.js /modules/user.js /modules/posts.js public/index.html public/user.html public/posts/html

和server.js将包含modules目录中的所有文件,其中一个可能看起来像

index.js

app.get("/", function(req, res) { render("index.html"); });

user.js的

app.get("/user", function(req, res) { render("user.html"); });

post.js

app.get("/post", function(req, res) { render("post.html"); });

Well, without seeing any of your code or knowing what framework you're using, it will be hard to be specific, but you'll want to

Render some HTML that includes a form on HTTP GET this is the simplest part. Just write a simple document with a and some tags. I assume you already have some web server that does this. Listen for POST requests. This is different from a GET and your server will likely have a different function to handle that request (because the logic is different). Handle the POST requests here you'll just want to pull out the name from the POST data and print it (or do whatever you want).

I recommend using expressjs for this since you probably don't want to write all of the code that handles the web server. With this framework, rendering HTMl is as simple as

app.get("/", function(request, response) { res.send(some_html); });

and handling the POST would be as simple as

app.post("/endpoint", function(request, response) { console.log(request.query.name); });

edit: to answer your question, it really isn't that hard to spread out the logic for handling multiple pages throughout separate files. What I normally do is have a server.js that sets up everything, and then will include the other files that handle the logic for other pages.

For example, if your app might be laid out like

server.js /modules/ /modules/index.js /modules/user.js /modules/posts.js public/index.html public/user.html public/posts/html

and server.js would include all of the files inside the modules directory, one of them might look like

index.js

app.get("/", function(req, res) { render("index.html"); });

user.js

app.get("/user", function(req, res) { render("user.html"); });

post.js

app.get("/post", function(req, res) { render("post.html"); });

更多推荐

本文发布于:2023-04-28 01:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329642.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   如何将   文件   HTML   js

发布评论

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

>www.elefans.com

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