Skip to content

Instantly share code, notes, and snippets.

@crashmax-dev
Last active July 16, 2023 07:05
Show Gist options
  • Save crashmax-dev/f3714029492ed5230c00f28cc3e4eb8e to your computer and use it in GitHub Desktop.
Save crashmax-dev/f3714029492ed5230c00f28cc3e4eb8e to your computer and use it in GitHub Desktop.
form-submit
<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>
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