我不是在谈论按钮标签。我说的是具有提交类型的输入标签。我正在使用 fs 加载我当前的页面。我正在尝试学习表达。
服务器.js:
var express = require('express');
var path = require('path');
var fs = require('fs');
var app = express();
app.use(express.static(path.join(__dirname+'/public/')));
app.use(require('body-parser').urlencoded({ extended: true }));
app.get('/', function(a,b,c){
fs.readFileSync('index.html');
});
app.post('/testaction', function(a,b){
console.log(a.body.log);
fs.readFileSync('public/Logged.html');
});
app.listen(3000);
索引.html:
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<form method="post" action="http://localhost:3000/testaction">
<h1>Log This:</h1>
<input type="text" name="log"/>
<input type="submit", value="Press Me!"/>
</form>
</body>
</html>
基本上你在这里真的不需要fs。
例如,app.get()
需要 2 个参数:您要处理的 URL 和一个带有 2 或 3 个参数的回调。
通常我们像这样使用它:app.get("/",(req,res)=>{});
(req = 请求,res = 响应(。
响应有一个发送文件的方法:http://expressjs.com/fr/4x/api.html#res.sendFile
现在对于您的问题,您可以将其作为 ajax 请求进行管理:
- 在索引中.html向
/testaction
发出 POST ajax 请求。 - 在服务器中.js只需在
app.post
中返回状态 - 创建另一条路线,例如
app.get("/test",(req,res)=>{res.sendFile("public/Logged.html");});
- 在 INDEX 中.html重定向到
/test
ajax 响应中的状态是否正常。