繁体   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