상세 컨텐츠

본문 제목

폼에 입력한 데이터를 서버에 전송하는 법 (POST요청)

Javascript

by 모모87 2021. 10. 4. 19:43

본문

server.js

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));

app.listen(8080, function () {
    console.log('listening on 8080')
});

app.get('/', function (요청, 응답) {
    응답.sendFile(__dirname + '/index.html')
})
app.get('/write', function (요청, 응답) {
    응답.sendFile(__dirname + '/write.html')
})


app.post('/add', function (요청, 응답) {
    응답.send('전송완료');
    console.log(요청.body.date);
    console.log(요청.body.title);
})

 

추가 설치:

npm i body-parser

 

 

write.html

<form action="/add" method="POST">
  <div class="form-label-group">
    <label for="toDo">할일</label>
    <input type="text" id="toDo" class="form-control" name="title" required="" autofocus="">
    </div>
    <div class="form-label-group mt-3">
    <label for="dueDate">due date</label>
    <input type="text" id="dueDate" class="form-control" required="" name="date">
  </div>
  <button class="btn btn-lg btn-danger btn-block mt-3" type="submit">Submit</button>
</form>

 

반응형

관련글 더보기