Я пытаюсь отправить данные формы на терминал и консоль, когда нажимаю кнопку отправки. Прямо сейчас я получаю ошибку 405. Я использую Linux Mint 19.2, vscode, node.js, express, и не совсем уверен, что я делаю неправильно.
Также, если у кого-то есть хорошие ресурсы по устранению неполадок с консолью, передайте их мне.
const express = require('express');
const app = express();
const path = require('path');
const PORT = 8080;
const log = console.log;
//Data Parsing(converts html data from the 'client,' to json for the server)
app.use(express.urlencoded({
extended: false
}));
app.use(express.json());
//Where the data(email) is sent from the client over route(/email) using a post method
app.post('/email', (req, res) => {
// TODO:
//SEND EMAIL HERE
log("Data: ", req.body);
res.json({
message: 'Message received!!!'
})
});
//Routing
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'index.html'));
});
//Port with Callback
app.listen(PORT, () => log('Server is listening on PORT, ', 8080));
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta id="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form class="contact-form" action="">
<input id="name" class="gutter" type="name" placeholder="Your Name">
<input id="company" type="text" placeholder="Company(optional)">
<input id="phone" class="gutter" type="number" placeholder="Your Telephone Number">
<input id="email" type="email" placeholder="Your Email Address">
<textarea id="text" type='text' col="30" rows="10" placeholder="Your message..."></textarea>
<input type="submit" value="Send">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="../assets/js/app.js"></script>
<script>
$("form").on("submit", (e) => {
e.preventDefault();
//Getting all data one at a time
const name = $("#name")
.val()
.trim();
const company = $("#company")
.val()
.trim();
const phone = $("#phone")
.val()
.trim();
const email = $("#email")
.val()
.trim();
const text = $("#text")
.val()
.trim();
//Storing all data into an object
const data = {
name,
company,
email,
phone,
text
};
//1st=route that server receives the response
//2nd=data to post
//3rd=callback
$.post("/email", data, function() {
console.log("Server received our data!");
});
});
</script>
</body>
</html>