[英]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文件,該文件將打印此用戶名。
怎么可能呢? 謝謝!
好吧,沒有看到你的任何代碼或知道你正在使用什么框架,它將很難具體,但你會想要
我建議使用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目錄中的所有文件,其中一個可能看起來像
app.get("/", function(req, res) {
render("index.html");
});
app.get("/user", function(req, res) {
render("user.html");
});
app.get("/post", function(req, res) {
render("post.html");
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.