Last active
July 16, 2023 07:05
-
-
Save crashmax-dev/f3714029492ed5230c00f28cc3e4eb8e to your computer and use it in GitHub Desktop.
form-submit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form method="POST" action="/login"> | |
<label for="username"> | |
Username: | |
<input name="username" type="text" required /> | |
</label> | |
<label for="password"> | |
Password: | |
<input name="password" type="password" required /> | |
</label> | |
<button type="submit">Login</button> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const bodyParser = require('body-parser') | |
const path = require('path') | |
const urlencodedParser = bodyParser.urlencoded({ extended: false }) | |
const app = express() | |
// методом GET по роуту localhost:3000/login отдается страница с формой | |
app.get('/login', (request, response) => { | |
response.sendFile(path.join(__dirname, '..', 'html', 'login.html')) | |
}) | |
// методом POST по роуту localhost:3000/login принимается отправленная форма | |
app.post('/login', urlencodedParser, (request, response) => { | |
const username = request.body.username | |
if (!username) { | |
return response.status(400).json({ error: 'Username is required' }) | |
} | |
const password = request.body.password | |
if (!password) { | |
return response.status(400).json({ error: 'Password is required' }) | |
} | |
// тут ходишь в базу, создаешь jwt и если всё ок | |
// то делаешь редирект на /profile (например) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment