자바스크립트 파일에서 html 파일로 변수 전달하는 방법이 있나요?

조회수 3528회

app.js 파일에서 main.html 의 email을 읽어준 뒤, email_reciver 으로 main.html에 email 변수로 값을 넘겨줘서 출력하고 싶은데, 여기 저기 찾아봐도 못 찾겠네요 ㅠㅠ ejs 경우에는 자바스크립트에서 넘겨준 변수를 <%= > 형식으로 읽어오는 것 같은데 html의 경우에는 어떻게 읽어오는지 궁금합니다.

app.js

const express = require('express');
const bodyparser= require('body-parser');
const app = express();

app.use(express.static('public'))
app.use(bodyparser.urlencoded({extended:false}))
app.use(bodyparser.json())
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);

app.listen(3000, ()=>{
    console.log('3000번 포트에 대기중!')
})




app.get('/main', function(req, res) {
    res.render('main.html')
})

app.post('/email_reciver', function(req, res){

    res.render('main.html',{'email': req.body.email})
})

main.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="unf-8">
    <title>main page</title>
</head>

<body>
    <h1>main page</h1>
    <img src="images/chrome.jpg">
    <p>
    <form action="/email_reciver" method="post">
        <p>
            <input type="text" name='email'>
        </p>
        <p>
            <input type="submit" value="제출">
        </p>
        </p>
        <script>

            if(email){
                document.write('hello!'+email)
            }
        </script>
</body>

</html>

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)