File size: 24,322 Bytes
1e2511f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 | const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const compression = require('compression');
const morgan = require('morgan');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const multer = require('multer');
const path = require('path');
const fs = require('fs').promises;
const crypto = require('crypto');
const app = express();
const config = {
port: process.env.PORT || 3000,
jwtSecret: process.env.JWT_SECRET || 'super-secret-key-change-in-production',
saltRounds: 12,
uploadPath: './uploads',
maxFileSize: 10 * 1024 * 1024,
allowedFileTypes: ['image/jpeg', 'image/png', 'image/gif', 'application/pdf', 'text/plain']
};
const database = {
users: [
{
id: 1,
username: 'admin',
email: 'admin@example.com',
password: '$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewwA8SoMG0qT5tA2',
role: 'admin',
isActive: true,
createdAt: new Date('2024-01-01'),
lastLogin: new Date(),
profile: {
firstName: 'Admin',
lastName: 'User',
avatar: null,
bio: 'System administrator'
}
},
{
id: 2,
username: 'user1',
email: 'user1@example.com',
password: '$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewwA8SoMG0qT5tA2',
role: 'user',
isActive: true,
createdAt: new Date('2024-01-15'),
lastLogin: new Date(),
profile: {
firstName: 'John',
lastName: 'Doe',
avatar: null,
bio: 'Regular user'
}
}
],
posts: [
{
id: 1,
title: 'Welcome to our API',
content: 'This is a sample post to demonstrate the API functionality.',
authorId: 1,
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-01'),
published: true,
tags: ['welcome', 'api', 'demo'],
likes: 5,
views: 100
}
],
comments: [
{
id: 1,
postId: 1,
authorId: 2,
content: 'Great post! Thanks for sharing.',
createdAt: new Date('2024-01-02'),
parentId: null,
likes: 2
}
],
sessions: []
};
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, config.uploadPath);
},
filename: (req, file, cb) => {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname));
}
});
const upload = multer({
storage: storage,
limits: {
fileSize: config.maxFileSize
},
fileFilter: (req, file, cb) => {
if (config.allowedFileTypes.includes(file.mimetype)) {
cb(null, true);
} else {
cb(new Error('Invalid file type'), false);
}
}
});
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
message: {
error: 'Too many requests from this IP, please try again later.'
}
});
const authLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 5,
message: {
error: 'Too many authentication attempts, please try again later.'
}
});
app.use(helmet());
app.use(cors({
origin: process.env.ALLOWED_ORIGINS ? process.env.ALLOWED_ORIGINS.split(',') : ['http://localhost:3000'],
credentials: true
}));
app.use(compression());
app.use(morgan('combined'));
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
app.use('/uploads', express.static(config.uploadPath));
app.use(limiter);
const authenticateToken = (req, res, next) => {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
if (!token) {
return res.status(401).json({ error: 'Access token required' });
}
jwt.verify(token, config.jwtSecret, (err, user) => {
if (err) {
return res.status(403).json({ error: 'Invalid or expired token' });
}
req.user = user;
next();
});
};
const requireRole = (roles) => {
return (req, res, next) => {
if (!req.user || !roles.includes(req.user.role)) {
return res.status(403).json({ error: 'Insufficient permissions' });
}
next();
};
};
const validateRequest = (schema) => {
return (req, res, next) => {
const { error } = schema.validate(req.body);
if (error) {
return res.status(400).json({
error: 'Validation failed',
details: error.details.map(detail => detail.message)
});
}
next();
};
};
const asyncHandler = (fn) => (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
const generateId = () => {
return Math.max(...database.users.map(u => u.id), ...database.posts.map(p => p.id), ...database.comments.map(c => c.id)) + 1;
};
const findUserById = (id) => {
return database.users.find(user => user.id === parseInt(id));
};
const findUserByUsername = (username) => {
return database.users.find(user => user.username === username);
};
const findUserByEmail = (email) => {
return database.users.find(user => user.email === email);
};
const findPostById = (id) => {
return database.posts.find(post => post.id === parseInt(id));
};
app.get('/api/health', (req, res) => {
res.json({
status: 'OK',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
memory: process.memoryUsage(),
version: '1.0.0'
});
});
app.post('/api/auth/register', authLimiter, asyncHandler(async (req, res) => {
const { username, email, password, firstName, lastName } = req.body;
if (!username || !email || !password) {
return res.status(400).json({ error: 'Username, email, and password are required' });
}
if (findUserByUsername(username)) {
return res.status(409).json({ error: 'Username already exists' });
}
if (findUserByEmail(email)) {
return res.status(409).json({ error: 'Email already exists' });
}
if (password.length < 8) {
return res.status(400).json({ error: 'Password must be at least 8 characters long' });
}
const hashedPassword = await bcrypt.hash(password, config.saltRounds);
const newUser = {
id: generateId(),
username,
email,
password: hashedPassword,
role: 'user',
isActive: true,
createdAt: new Date(),
lastLogin: null,
profile: {
firstName: firstName || '',
lastName: lastName || '',
avatar: null,
bio: ''
}
};
database.users.push(newUser);
const token = jwt.sign(
{ userId: newUser.id, username: newUser.username, role: newUser.role },
config.jwtSecret,
{ expiresIn: '24h' }
);
const { password: _, ...userWithoutPassword } = newUser;
res.status(201).json({
message: 'User registered successfully',
user: userWithoutPassword,
token
});
}));
app.post('/api/auth/login', authLimiter, asyncHandler(async (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).json({ error: 'Username and password are required' });
}
const user = findUserByUsername(username) || findUserByEmail(username);
if (!user) {
return res.status(401).json({ error: 'Invalid credentials' });
}
if (!user.isActive) {
return res.status(401).json({ error: 'Account is deactivated' });
}
const isPasswordValid = await bcrypt.compare(password, user.password);
if (!isPasswordValid) {
return res.status(401).json({ error: 'Invalid credentials' });
}
user.lastLogin = new Date();
const token = jwt.sign(
{ userId: user.id, username: user.username, role: user.role },
config.jwtSecret,
{ expiresIn: '24h' }
);
const sessionId = crypto.randomUUID();
database.sessions.push({
id: sessionId,
userId: user.id,
token,
createdAt: new Date(),
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000)
});
const { password: _, ...userWithoutPassword } = user;
res.json({
message: 'Login successful',
user: userWithoutPassword,
token,
sessionId
});
}));
app.post('/api/auth/logout', authenticateToken, (req, res) => {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
database.sessions = database.sessions.filter(session => session.token !== token);
res.json({ message: 'Logout successful' });
});
app.get('/api/auth/me', authenticateToken, (req, res) => {
const user = findUserById(req.user.userId);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
const { password: _, ...userWithoutPassword } = user;
res.json(userWithoutPassword);
});
app.get('/api/users', authenticateToken, requireRole(['admin']), (req, res) => {
const { page = 1, limit = 10, search = '', role = '' } = req.query;
const offset = (parseInt(page) - 1) * parseInt(limit);
let filteredUsers = database.users.filter(user => {
const matchesSearch = !search ||
user.username.toLowerCase().includes(search.toLowerCase()) ||
user.email.toLowerCase().includes(search.toLowerCase()) ||
user.profile.firstName.toLowerCase().includes(search.toLowerCase()) ||
user.profile.lastName.toLowerCase().includes(search.toLowerCase());
const matchesRole = !role || user.role === role;
return matchesSearch && matchesRole;
});
const total = filteredUsers.length;
const users = filteredUsers
.slice(offset, offset + parseInt(limit))
.map(({ password, ...user }) => user);
res.json({
users,
pagination: {
page: parseInt(page),
limit: parseInt(limit),
total,
pages: Math.ceil(total / parseInt(limit))
}
});
});
app.get('/api/users/:id', authenticateToken, (req, res) => {
const userId = parseInt(req.params.id);
const user = findUserById(userId);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
if (req.user.userId !== userId && req.user.role !== 'admin') {
return res.status(403).json({ error: 'Access denied' });
}
const { password: _, ...userWithoutPassword } = user;
res.json(userWithoutPassword);
});
app.put('/api/users/:id', authenticateToken, upload.single('avatar'), asyncHandler(async (req, res) => {
const userId = parseInt(req.params.id);
const user = findUserById(userId);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
if (req.user.userId !== userId && req.user.role !== 'admin') {
return res.status(403).json({ error: 'Access denied' });
}
const { firstName, lastName, bio, email } = req.body;
if (email && email !== user.email) {
const existingUser = findUserByEmail(email);
if (existingUser && existingUser.id !== userId) {
return res.status(409).json({ error: 'Email already exists' });
}
user.email = email;
}
if (firstName !== undefined) user.profile.firstName = firstName;
if (lastName !== undefined) user.profile.lastName = lastName;
if (bio !== undefined) user.profile.bio = bio;
if (req.file) {
if (user.profile.avatar) {
try {
await fs.unlink(path.join(config.uploadPath, path.basename(user.profile.avatar)));
} catch (error) {
console.error('Failed to delete old avatar:', error);
}
}
user.profile.avatar = `/uploads/${req.file.filename}`;
}
const { password: _, ...userWithoutPassword } = user;
res.json({
message: 'User updated successfully',
user: userWithoutPassword
});
}));
app.delete('/api/users/:id', authenticateToken, requireRole(['admin']), (req, res) => {
const userId = parseInt(req.params.id);
const userIndex = database.users.findIndex(user => user.id === userId);
if (userIndex === -1) {
return res.status(404).json({ error: 'User not found' });
}
if (userId === req.user.userId) {
return res.status(400).json({ error: 'Cannot delete your own account' });
}
database.users.splice(userIndex, 1);
database.sessions = database.sessions.filter(session => session.userId !== userId);
res.json({ message: 'User deleted successfully' });
});
app.get('/api/posts', (req, res) => {
const { page = 1, limit = 10, search = '', tag = '', authorId = '' } = req.query;
const offset = (parseInt(page) - 1) * parseInt(limit);
let filteredPosts = database.posts.filter(post => {
if (!post.published && (!req.user || req.user.userId !== post.authorId)) {
return false;
}
const matchesSearch = !search ||
post.title.toLowerCase().includes(search.toLowerCase()) ||
post.content.toLowerCase().includes(search.toLowerCase());
const matchesTag = !tag || post.tags.includes(tag);
const matchesAuthor = !authorId || post.authorId === parseInt(authorId);
return matchesSearch && matchesTag && matchesAuthor;
});
const total = filteredPosts.length;
const posts = filteredPosts
.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
.slice(offset, offset + parseInt(limit))
.map(post => {
const author = findUserById(post.authorId);
return {
...post,
author: author ? {
id: author.id,
username: author.username,
profile: author.profile
} : null
};
});
res.json({
posts,
pagination: {
page: parseInt(page),
limit: parseInt(limit),
total,
pages: Math.ceil(total / parseInt(limit))
}
});
});
app.get('/api/posts/:id', (req, res) => {
const postId = parseInt(req.params.id);
const post = findPostById(postId);
if (!post) {
return res.status(404).json({ error: 'Post not found' });
}
if (!post.published && (!req.user || req.user.userId !== post.authorId)) {
return res.status(404).json({ error: 'Post not found' });
}
post.views += 1;
const author = findUserById(post.authorId);
const comments = database.comments
.filter(comment => comment.postId === postId)
.map(comment => {
const commentAuthor = findUserById(comment.authorId);
return {
...comment,
author: commentAuthor ? {
id: commentAuthor.id,
username: commentAuthor.username,
profile: commentAuthor.profile
} : null
};
});
res.json({
...post,
author: author ? {
id: author.id,
username: author.username,
profile: author.profile
} : null,
comments
});
});
app.post('/api/posts', authenticateToken, (req, res) => {
const { title, content, tags = [], published = false } = req.body;
if (!title || !content) {
return res.status(400).json({ error: 'Title and content are required' });
}
const newPost = {
id: generateId(),
title,
content,
authorId: req.user.userId,
createdAt: new Date(),
updatedAt: new Date(),
published,
tags: Array.isArray(tags) ? tags : [],
likes: 0,
views: 0
};
database.posts.push(newPost);
const author = findUserById(newPost.authorId);
res.status(201).json({
message: 'Post created successfully',
post: {
...newPost,
author: author ? {
id: author.id,
username: author.username,
profile: author.profile
} : null
}
});
});
app.put('/api/posts/:id', authenticateToken, (req, res) => {
const postId = parseInt(req.params.id);
const post = findPostById(postId);
if (!post) {
return res.status(404).json({ error: 'Post not found' });
}
if (post.authorId !== req.user.userId && req.user.role !== 'admin') {
return res.status(403).json({ error: 'Access denied' });
}
const { title, content, tags, published } = req.body;
if (title !== undefined) post.title = title;
if (content !== undefined) post.content = content;
if (tags !== undefined) post.tags = Array.isArray(tags) ? tags : [];
if (published !== undefined) post.published = published;
post.updatedAt = new Date();
const author = findUserById(post.authorId);
res.json({
message: 'Post updated successfully',
post: {
...post,
author: author ? {
id: author.id,
username: author.username,
profile: author.profile
} : null
}
});
});
app.delete('/api/posts/:id', authenticateToken, (req, res) => {
const postId = parseInt(req.params.id);
const postIndex = database.posts.findIndex(post => post.id === postId);
if (postIndex === -1) {
return res.status(404).json({ error: 'Post not found' });
}
const post = database.posts[postIndex];
if (post.authorId !== req.user.userId && req.user.role !== 'admin') {
return res.status(403).json({ error: 'Access denied' });
}
database.posts.splice(postIndex, 1);
database.comments = database.comments.filter(comment => comment.postId !== postId);
res.json({ message: 'Post deleted successfully' });
});
app.post('/api/posts/:id/like', authenticateToken, (req, res) => {
const postId = parseInt(req.params.id);
const post = findPostById(postId);
if (!post) {
return res.status(404).json({ error: 'Post not found' });
}
post.likes += 1;
res.json({
message: 'Post liked successfully',
likes: post.likes
});
});
app.post('/api/posts/:id/comments', authenticateToken, (req, res) => {
const postId = parseInt(req.params.id);
const post = findPostById(postId);
if (!post) {
return res.status(404).json({ error: 'Post not found' });
}
const { content, parentId = null } = req.body;
if (!content) {
return res.status(400).json({ error: 'Comment content is required' });
}
if (parentId) {
const parentComment = database.comments.find(c => c.id === parseInt(parentId) && c.postId === postId);
if (!parentComment) {
return res.status(404).json({ error: 'Parent comment not found' });
}
}
const newComment = {
id: generateId(),
postId,
authorId: req.user.userId,
content,
createdAt: new Date(),
parentId: parentId ? parseInt(parentId) : null,
likes: 0
};
database.comments.push(newComment);
const author = findUserById(newComment.authorId);
res.status(201).json({
message: 'Comment created successfully',
comment: {
...newComment,
author: author ? {
id: author.id,
username: author.username,
profile: author.profile
} : null
}
});
});
app.get('/api/analytics', authenticateToken, requireRole(['admin']), (req, res) => {
const totalUsers = database.users.length;
const activeUsers = database.users.filter(user => user.isActive).length;
const totalPosts = database.posts.length;
const publishedPosts = database.posts.filter(post => post.published).length;
const totalComments = database.comments.length;
const activeSessions = database.sessions.filter(session => session.expiresAt > new Date()).length;
const usersByRole = database.users.reduce((acc, user) => {
acc[user.role] = (acc[user.role] || 0) + 1;
return acc;
}, {});
const postsLast30Days = database.posts.filter(post => {
const thirtyDaysAgo = new Date();
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
return post.createdAt >= thirtyDaysAgo;
}).length;
res.json({
users: {
total: totalUsers,
active: activeUsers,
byRole: usersByRole
},
posts: {
total: totalPosts,
published: publishedPosts,
last30Days: postsLast30Days
},
comments: {
total: totalComments
},
sessions: {
active: activeSessions
}
});
});
app.use((err, req, res, next) => {
console.error('Error:', err);
if (err instanceof multer.MulterError) {
if (err.code === 'LIMIT_FILE_SIZE') {
return res.status(400).json({ error: 'File size too large' });
}
return res.status(400).json({ error: 'File upload error' });
}
if (err.message === 'Invalid file type') {
return res.status(400).json({ error: 'Invalid file type' });
}
res.status(500).json({
error: 'Internal server error',
message: process.env.NODE_ENV === 'development' ? err.message : 'Something went wrong'
});
});
app.use('*', (req, res) => {
res.status(404).json({
error: 'Endpoint not found',
availableEndpoints: {
auth: [
'POST /api/auth/register',
'POST /api/auth/login',
'POST /api/auth/logout',
'GET /api/auth/me'
],
users: [
'GET /api/users',
'GET /api/users/:id',
'PUT /api/users/:id',
'DELETE /api/users/:id'
],
posts: [
'GET /api/posts',
'GET /api/posts/:id',
'POST /api/posts',
'PUT /api/posts/:id',
'DELETE /api/posts/:id',
'POST /api/posts/:id/like',
'POST /api/posts/:id/comments'
],
other: [
'GET /api/health',
'GET /api/analytics'
]
}
});
});
const startServer = async () => {
try {
await fs.access(config.uploadPath);
} catch {
await fs.mkdir(config.uploadPath, { recursive: true });
console.log(`Created upload directory: ${config.uploadPath}`);
}
app.listen(config.port, () => {
console.log(`Server running on port ${config.port}`);
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
console.log(`Upload path: ${config.uploadPath}`);
console.log(`Max file size: ${config.maxFileSize / 1024 / 1024}MB`);
});
};
if (require.main === module) {
startServer();
}
module.exports = app; |