[英]Show new html page after post request?
First I want to describe, how the project should work.首先我想描述一下这个项目应该如何工作。 I want to build my own little website with html, css, javascript (and expressJS and nodeJS).
我想用 html、css、javascript(以及 expressJS 和 nodeJS)建立我自己的小网站。 I´ve also watched a few tutorials about Rest-APIs and how they work.
我还看过一些关于 Rest-API 及其工作原理的教程。 I want to create a login page , where you should enter a password to get to the "main- page".
我想创建一个登录页面,您应该在其中输入密码才能进入“主页”。 The problem is, that I´m working with a post-request atfer the user has clicked on a "submit" - button, where the value of a password box will be send to a server to check.
问题是,我正在处理用户单击“提交”按钮后的请求后,密码框的值将被发送到服务器进行检查。 After this was executed, I dont know how to show the new html page and how to redirect the user to a new page .
执行此操作后,我不知道如何显示新的 html 页面以及如何将用户重定向到新页面。
I´ve tried two different things.我试过两种不同的东西。 Here is my beginning of the code in my server.js:
这是我的 server.js 中代码的开头:
var app = express();
var server = app.listen(3000);
app.use(express.static('website'));
First I wanted to send a file after checking the password in my post request:首先,我想在我的帖子请求中检查密码后发送一个文件:
app.post('/all', function(req, res) {
if(req.body.password != 'example') {
return
}
res.sendFile('index.html');
});
I found this github issue, where a user has the same problem: https://github.com/expressjs/express/issues/692我发现了这个 github 问题,其中用户有同样的问题: https://github.com/expressjs/express/issues/692
But this method doesnt seem to work in a post-request.但是这种方法似乎不适用于后请求。 So I´ve tried another thing.
所以我尝试了另一件事。 I already knew, that sendFile() works in a get-request (as I mentioned in a github issue), so I´ve made this:
我已经知道, sendFile()在获取请求中起作用(正如我在 github 问题中提到的那样),所以我做了这个:
app.get('/all', function(req, res) {
res.sendFile('index.html');
});
app.post('/register', function(req, res) {
if(req.body.password != 'example') {
return
}
res.redirect('/all')
});
For this code example I used the following stack overflow page: How to redirect to another page after serving a post request in Node.js?对于此代码示例,我使用了以下堆栈溢出页面: How to redirect to another page after serving a post request in Node.js? but it didnt work either.
但它也没有工作。
Script in html doc: html 文档中的脚本:
function XHTML_POST(path, parms, callback) {
req = new XMLHttpRequest();
var ret = false;
callbackPOST = callback;
req.open("POST", path, true);
req.onload = function () {
if (req.status != 200 && req.status != 1223 && req.status != 0 && req.status != 204) {
if (req.status == 401) { }
else { }
}
else {
callbackPOST = callback;
}
}
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
req.setRequestHeader("Cache-Control", "no-cache");
req.send(parms);
}
function postPass() {
var pass = document.getElementById('pwBox').value; //id of passwordbox
XHTML_POST('/register', 'password=' + pass, callback)
}
function callback() { }
</script>
After clicking on a submit button the postPass() function will be executed.单击提交按钮后,将执行postPass() function。 Hopefully you understood my problem and what I´m trying to make.
希望你能理解我的问题以及我想要做什么。 Thanks for reading it.
感谢您阅读。
You can use the html forms:您可以使用 html forms:
HTML: HTML:
<form method="POST">
<input type="email" name="youremail" placeholder="Email Address" required>
<input type="text" name="yourname" placeholder="Name" required>
<input class="subscribe-button" type="submit" value="Subscribe">
</form>
NodeJs Server: NodeJs 服务器:
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, 'login.html'))
})
app.post("/", (req, res) => {
//Your Password Verification
if(true){
res.sendFile(path.join(__dirname, 'index.html'))
}else{
res.sendFile(path.join(__dirname, 'login.html'))
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.