簡體   English   中英

發布請求后重新加載index.html

[英]Reload index.html after post request

我想用express和node.js編寫一個非常簡單的Web應用程序。 該應用程序只有帶有表單的index.html。 表單過帳后,node.js服務器應作出反應,將輸入值寫入txt文件中。 然后,在瀏覽器中,應該重新加載index.html文件,以准備提交下一個表單。

我設法使一切正常工作,但處理請求后重新加載index.html文件的部分除外。 index.html位於“ www”文件夾中。

最好的方法是什么?

這是我的app.js:

var express = require('express')
var app = express()
const fs = require('fs');
const bodyParser = require('body-parser');

app.use(express.static('www'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());


var server = app.listen(3000, function () {
    var host = server.address().address
    var port = server.address().port
    console.log('Express app listening at http://%s:%s', host, port)
})

server.on('request', (req, res) => {
   if (req.method === 'POST') {
        collectRequestData(req, res => {
            console.log(res);
        });
   } 
   // Here the index.html should be reloaded
});

//This function only writes the form data to txt-file, I don't know if it is relevant here
function collectRequestData(request, callback) {}

處理完帖子后,嘗試重定向到索引:

server.on('request', (req, res) => {
   if (req.method === 'POST') {
        collectRequestData(req, res => {
            console.log(res);
        });
   } 
   // Here the index.html should be reloaded
 res.redirect('/');; --->Redirect to index here.
});

這是參考: https : //expressjs.com/en/4x/api.html#res.redirect

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM