пишу сайт, бэк которого на node.js
, а фронт - js
, html
, css
. Научился запускать сервер node.js
, но пока не понял как сочетать его с клиентским js. Как я понял, всё пишется в одном файле, но как быть, если я хочу обратиться к объекту document
, а мне выдаётся ошибка ReferenceError: document is not defined
?
Подскажите, пожалуйста, как на этом примере решить вопрос сочетания серверного и клиентского javascript.
Это часть моего файла index.html:
<head>
...
<script src="../index.js"></script>
</head>
<body>
<div class="row">
...
<input id="email" type="email" class="validate">
...
</div>
<div class="row">
...
<input id="password" type="password" class="validate">
...
</div>
<div class="row">
<a id="reg" class="waves-effect waves-light btn-small" href="main.html">Зарегистрироваться</a>
</div>
</body>
Это часть моего файла index.js:
const http = require('http')
const path = require('path')
const server = http.createServer((req, res) => {
let filePath = path.join(__dirname, 'public', req.url === '/' ? 'index.html' : req.url)
// ...
})
// Попытка получить логин с паролем из html и вывести их
const btn_reg = document.getElementById('reg')
const input_email = document.getElementById('email').value
const input_pass = document.getElementById('password').value
btn_reg.addEventListener('click', () => {
console.log("email: ", input_email, "\n", "password: ", input_pass)
})
const PORT = process.env.PORT || 3000
server.listen(PORT, () => {
console.log(`Server has been started on ${PORT}...`)
})