发布请求后重新加载index.html

时间:2018-05-15 15:01:13

标签: javascript node.js express

我想用express和node.js编写一个非常简单的web应用程序。 该应用只有一个带表单的index.html。当表单被POST时,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) {}

1 个答案:

答案 0 :(得分:-1)

在处理帖子后尝试重定向到索引:

[1-9]\d*(\.\d+)?$

以下是参考:https://expressjs.com/en/4x/api.html#res.redirect

相关问题