// Master backend development with REST APIs, databases, authentication, and server frameworks. Use when building APIs, working with databases, or implementing backend logic.
| name | backend-technologies |
| description | Master backend development with REST APIs, databases, authentication, and server frameworks. Use when building APIs, working with databases, or implementing backend logic. |
Comprehensive guide to backend development and API design.
const express = require('express');
const app = express();
app.use(express.json());
// GET endpoint
app.get('/api/users/:id', (req, res) => {
const { id } = req.params;
res.json({ id, name: 'John Doe' });
});
// POST endpoint
app.post('/api/users', (req, res) => {
const user = req.body;
res.status(201).json(user);
});
// Error handling
app.use((err, req, res, next) => {
res.status(500).json({ error: err.message });
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
-- Create table
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Query with joins
SELECT u.name, COUNT(p.id) as post_count
FROM users u
LEFT JOIN posts p ON u.id = p.user_id
GROUP BY u.id
ORDER BY post_count DESC;
-- Transactions
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
const jwt = require('jsonwebtoken');
// Generate token
function generateToken(userId) {
return jwt.sign(
{ userId },
process.env.JWT_SECRET,
{ expiresIn: '24h' }
);
}
// Verify token middleware
function authMiddleware(req, res, next) {
const token = req.headers.authorization?.split(' ')[1];
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET);
req.userId = decoded.userId;
next();
} catch (error) {
res.status(401).json({ error: 'Invalid token' });
}
}