提问者:小点点

将用户输入从表单插入hbs


我正在尝试插入用户的信息到我的模板文件后,他们已经注册或登录。

例如,在用户登录后,我想将他们的名字提取到仪表板模板中。 下面是我的代码:

// Require modules
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const port = process.env.PORT || 3000
// Create the express server
const app = express();

// Use the bodyParser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

// Serve the static files
app.use(express.static(path.join(__dirname, 'static')));

// Set the view engine
app.set('view engine', 'hbs');

// Making a get request to a template
app.get('/dashboard', (req, res) =>{
    res.render('dashboard', {
        username: req.body.username
    })
})

// make a post request
app.post('/users/signin', (req, res)=>{
    app.use('/users/signin', (req, res) =>{
        res.setHeader('Content-Type', 'application/json');
    app.get('/dashboard', (req, res) => {
        res.render('dashboard', JSON.stringify({
            user:{
                username: req.body.username
            }
        }))
    })
    })
})

// listen on a port
app.listen(port, ()=> console.log(`Server is up and running on port ${port}`));

共1个答案

匿名用户

尝试编辑此行

res.render('dashboard', {user: req.body.username} );

在模板端,在要放置username的位置使用{{user}}

对于。

<h1>Welcome {{user}}</h1>