Middleware ⚡️ Node.js с примерами кода #54
Replies: 1 comment
-
//sql CREATE TABLE rooms ( id SERIAL PRIMARY KEY, number VARCHAR(50) NOT NULL, type VARCHAR(50) NOT NULL, price DECIMAL NOT NULL ); CREATE TABLE bookings ( id SERIAL PRIMARY KEY, room_id INT REFERENCES rooms(id), guest_name VARCHAR(100) NOT NULL, check_in_date DATE NOT NULL, check_out_date DATE NOT NULL ); CREATE TABLE users ( id SERIAL PRIMARY KEY, username VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL ); INSERT INTO rooms (id, number, type, price) VALUES (1, 101, 'Single', 100.00); INSERT INTO rooms (id, number, type, price) VALUES (2, 102, 'Double', 150.00); INSERT INTO rooms (id, number, type, price) VALUES (3, 103, 'Suite', 200.00); //server.js const express = require('express'); const { Pool } = require('pg'); const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken'); const path = require('path'); const app = express(); const port = 3000; const pool = new Pool({ user: process.env.DB_USER, host: process.env.DB_HOST, database: process.env.DB_NAME, password: process.env.DB_PASSWORD, port: process.env.DB_PORT, }); app.use(express.json()); app.use(express.static(path.join(__dirname, 'public'))); app.get('/', (req, res) => { res.sendFile(path.join(__dirname, 'public', 'index.html')); }); pool.connect((err) => { if (err) {
} else {
} }); // Маршруты для добавления данных app.post('/rooms', async (req, res) => { const { number, type, price } = req.body; const result = await pool.query(
); res.json(result.rows[0]); }); app.post('/bookings', async (req, res) => { const { room_id, guest_name, check_in_date, check_out_date } = req.body; const result = await pool.query(
); res.json(result.rows[0]); }); // Маршруты для изменения данных app.put('/rooms/:id', async (req, res) => { const { id } = req.params; const { number, type, price } = req.body; const result = await pool.query(
); res.json(result.rows[0]); }); app.put('/bookings/:id', async (req, res) => { const { id } = req.params; const { room_id, guest_name, check_in_date, check_out_date } = req.body; const result = await pool.query(
); res.json(result.rows[0]); }); // Маршруты для удаления данных app.delete('/rooms/:id', async (req, res) => { const { id } = req.params; await pool.query('DELETE FROM rooms WHERE id = $1', [id]); res.sendStatus(204); }); app.delete('/bookings/:id', async (req, res) => { const { id } = req.params; await pool.query('DELETE FROM bookings WHERE id = $1', [id]); res.sendStatus(204); }); // Маршруты для вывода данных app.get('/rooms', async (req, res) => { const result = await pool.query('SELECT * FROM rooms'); res.json(result.rows); }); app.get('/bookings', async (req, res) => { const result = await pool.query('SELECT * FROM bookings'); res.json(result.rows); }); // Маршруты для регистрации и авторизации app.post('/register', async (req, res) => { const { username, password } = req.body; const hashedPassword = await bcrypt.hash(password, 10); const result = await pool.query(
); res.json(result.rows[0]); }); app.post('/login', async (req, res) => { const { username, password } = req.body; const result = await pool.query('SELECT * FROM users WHERE username = $1', [username]); const user = result.rows[0]; if (user && await bcrypt.compare(password, user.password)) {
} else {
} }); // Middleware для проверки токена const authenticateToken = (req, res, next) => { const token = req.headers['authorization']; if (!token) return res.sendStatus(401); jwt.verify(token, process.env.JWT_SECRET, (err, user) => {
}); }; // Пример защищенного маршрута app.get('/protected', authenticateToken, (req, res) => { res.send('This is a protected route'); }); app.listen(port, () => { console.log( }); app.get('/rooms', async (req, res) => { try {
} catch (err) {
} }); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Middleware ⚡️ Node.js с примерами кода
Справочник Node.js с примерами кода
https://nodejsdev.ru/guides/webdraftt/middleware/
Beta Was this translation helpful? Give feedback.
All reactions