gpt4 book ai didi

html - 仅 Express 加载主页

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:22 24 4
gpt4 key购买 nike

我正在努力学习 express 。我正在使用 FS 加载我的 HTML 页面。我通过 google 搜索唯一能找到的就是使用 ASP.NET 而不是 Express。

服务器.js:

var express = require('express');
var path = require('path');
var fs = require('fs');

var app = express();

app.use(require('body-parser').urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname+'/')))

app.get('/', function(a,b,c){
fs.readFileSync('index.html');
});

app.post('/testaction', function(a,b){
console.log(a.body.log);
fs.readFileSync('Logged.html');
});

app.listen(3000);

index.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('/', function(req, res, next) {
res.render('index');
});

如果需要重定向,可以使用:

res.redirect('/staticFile.html');

关于html - 仅 Express 加载主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43377544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com