【发布时间】:2023-03-10 15:38:01
【问题描述】:
我最近从开始学习 PHP 转向 NodeJS,因为我对 JS 有更多的了解。我的问题是如何将发布的表单数据显示到 HTML 文件中?
server.js
const app = require('express')(),
bodyParser = require('body-parser'),
path = require('path');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'html/index.html')));
app.post('/student', (req, res) => res.send(req.body.user.name));
app.listen(3000, (req, res) => console.log('Listening on port 3000.'));
html/index.html
<body>
<form method='post' action='post'>
<input type='text' name = 'user[name]'>
<input type='submit' value='submit'>
</form>
</body>
但是在 post 方法中,我想发送一个 HTML 文件,而不是 req.body.user.name,我显然可以像在主页 (/) 上那样做,我想能够在新的 HTML 文件中包含一些变量,可能类似于以下内容:
<body>
<h1><? req.body.user.name + 's page. ?></h1>
<!-- rest of code -->
</body>
【问题讨论】:
标签: javascript html node.js server