-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
434 lines (353 loc) · 10.9 KB
/
setup.sh
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
#!/bin/bash
# Function to show loading message
show_loader() {
echo -n "Installing dependencies"
while :; do
echo -n "."
sleep 1
done &
LOADER_PID=$!
}
# Function to stop the loader
stop_loader() {
kill "$LOADER_PID"
wait "$LOADER_PID" 2>/dev/null
echo -e "\nDependencies installed successfully!"
}
# Ask for project name
read -p "Enter your project name: " projectName
# Create project directory
mkdir -p $projectName
cd $projectName
# Initialize npm project
npm init -y
# Create package.json with specified structure
cat <<EOL > package.json
{
"name": "$projectName",
"version": "1.0.0",
"description": "",
"main": "server.js",
"type": "module",
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"prod": "NODE_ENV=production node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "",
"cors": "",
"dotenv": "",
"express": "",
"express-async-errors": "",
"express-rate-limit": "",
"helmet": "",
"http-status-codes": "",
"jsonwebtoken": "",
"mongoose": "",
"morgan": ""
},
"devDependencies": {
"nodemon": ""
}
}
EOL
show_loader
# Install necessary packages
npm install express mongoose bcryptjs jsonwebtoken dotenv cors express-rate-limit helmet morgan http-status-codes express-async-errors
stop_loader
# Create folders for MVC structure, middleware, and config
mkdir -p controllers models routes utils middlewares logs config public
# Create .env file
cat <<EOL > .env
APP_URL=http://localhost:3000
PORT=3000
MONGO_URI=mongodb://127.0.0.1:27017
JWT_SECRET=your_jwt_secret_key
JWT_EXPIRES_IN=1h
EOL
# Create .gitignore
cat <<EOL > .gitignore
# npm
node_modules
package-lock.json
npm-shrinkwrap.json
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
EOL
# Create app.js
cat <<EOL > app.js
import express from 'express';
import helmet from 'helmet';
import cors from 'cors';
import morgan from 'morgan';
import path from 'path';
import rateLimit from 'express-rate-limit';
import { errorHandler } from './middlewares/errorHandler.js';
import authRoutes from './routes/authRoutes.js';
import connectDB from './config/connectDB.js';
import { AppError } from './utils/AppError.js';
import dotenv from 'dotenv';
// Load environment variables
dotenv.config();
// Connect to DB
connectDB();
const app = express();
// Set security HTTP headers
app.use(helmet());
// Enable CORS
app.use(cors());
// Rate limiter to prevent DOS attacks
const limiter = rateLimit({
max: 100,
windowMs: 60 * 60 * 1000, // 1 hour
message: 'Too many requests from this IP, please try again later'
});
app.use('/api', limiter);
// Development logging
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
// Parse JSON request bodies
app.use(express.json());
// Serve static files from the "public" folder
const __dirname = path.resolve(); // Get the current directory
app.use(express.static(path.join(__dirname, 'public'))); // Serve the "public" directory
// Routes
app.use('/api/v1/auth', authRoutes);
// Handle unknown routes
app.all('*', (req, res, next) => {
const err = new AppError(\`Can't find \${req.originalUrl} on this server!\`, 404);
next(err);
});
// Global error handling middleware
app.use(errorHandler);
export default app;
EOL
# Create server.js
cat <<EOL > server.js
import fs from 'fs';
import path from 'path';
import dotenv from 'dotenv';
import { fileURLToPath } from 'url';
import app from './app.js';
import { PORT } from './config/constants.js';
// Load environment variables
dotenv.config();
// Get the current directory path
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const port = PORT || 3000;
const server = app.listen(port, () => {
console.log(\`App running on http://localhost:\${port}\`);
});
// Error logging for production
process.on('unhandledRejection', (err) => {
console.error('Unhandled rejection! Shutting down...');
fs.appendFileSync(path.join(__dirname, 'logs', 'error.log'), \`\${new Date()} - Unhandled Rejection: \${err.message}\\n\`);
server.close(() => {
process.exit(1);
});
});
EOL
# Create authController.js
cat <<EOL > controllers/authController.js
import jwt from 'jsonwebtoken';
import User from '../models/userModel.js';
import { AppError } from '../utils/AppError.js';
import catchAsync from '../utils/catchAsync.js';
import { StatusCodes } from 'http-status-codes';
import { JWT_EXPIRES_IN, JWT_SECRET } from '../config/constants.js';
const signToken = (id, sessionId) => {
return jwt.sign({ id, sessionId }, JWT_SECRET, { expiresIn: JWT_EXPIRES_IN });
};
export const login = catchAsync(async (req, res, next) => {
const { email, password } = req.body;
if (!email || !password) {
return next(new AppError('Please provide email and password', StatusCodes.BAD_REQUEST));
}
const user = await User.findOne({ email }).select('+password');
if (!user || !(await user.correctPassword(password, user.password))) {
return next(new AppError('Incorrect email or password', StatusCodes.UNAUTHORIZED));
}
const sessionId = Date.now().toString();
user.sessionId = sessionId;
await user.save();
const token = signToken(user._id, sessionId);
res.status(StatusCodes.OK).json({ status: 'success', token });
});
export const protect = catchAsync(async (req, res, next) => {
let token;
if (req.headers.authorization && req.headers.authorization.startsWith('Bearer')) {
token = req.headers.authorization.split(' ')[1];
}
if (!token) {
return next(new AppError('You are not logged in! Please log in to get access', StatusCodes.UNAUTHORIZED));
}
const decoded = jwt.verify(token, JWT_SECRET);
const currentUser = await User.findById(decoded.id);
if (!currentUser) {
return next(new AppError('The user belonging to this token no longer exists.', StatusCodes.UNAUTHORIZED));
}
if (currentUser.sessionId !== decoded.sessionId) {
return next(new AppError('This token has been invalidated. Please log in again.', StatusCodes.UNAUTHORIZED));
}
req.user = currentUser;
next();
});
EOL
# Create userModel.js
cat <<EOL > models/userModel.js
import mongoose from 'mongoose';
import bcrypt from 'bcryptjs';
const userSchema = new mongoose.Schema({
email: {
type: String,
required: [true, 'Please provide your email'],
unique: true,
lowercase: true,
},
password: {
type: String,
required: [true, 'Please provide a password'],
minlength: 8,
select: false,
},
sessionId: String,
});
userSchema.pre('save', async function (next) {
if (!this.isModified('password')) return next();
this.password = await bcrypt.hash(this.password, 12);
next();
});
userSchema.methods.correctPassword = async function (candidatePassword, userPassword) {
return await bcrypt.compare(candidatePassword, userPassword);
};
const User = mongoose.model('User', userSchema);
export default User;
EOL
# Create authRoutes.js
cat <<EOL > routes/authRoutes.js
import express from 'express';
import { login, protect } from '../controllers/authController.js';
const router = express.Router();
router.post('/login', login);
router.get('/protected', protect, (req, res) => {
res.status(200).json({ message: 'Access granted to protected route' });
});
export default router;
EOL
# Create errorHandler.js in middlewares folder
cat <<EOL > middlewares/errorHandler.js
import { StatusCodes } from 'http-status-codes';
import { fileURLToPath } from 'url';
import fs from 'fs';
import path from 'path';
// Get the current directory path
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export const errorHandler = (err, req, res, next) => {
err.statusCode = err.statusCode || StatusCodes.INTERNAL_SERVER_ERROR;
err.status = err.status || 'error';
// Log error in production
if (process.env.NODE_ENV === 'production') {
fs.appendFileSync(path.join(__dirname, '..', 'logs', 'error.log'), \`\${new Date()} - \${err.message}\n \nStack: \${err.stack}\n\n---------------------------- X--------------------------------------- \n\n\`);
return res.status(err.statusCode).json({
status: err.status,
message: err.message,
});
}
return res.status(err.statusCode).json({
status: err.status,
message: err.message,
stack: err.stack, // Include stack trace in development
error: err, // Full error object for development
});
};
EOL
# Create AppError.js in utils folder
cat <<EOL > utils/AppError.js
export class AppError extends Error {
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode;
this.status = \`\${statusCode}\`.startsWith('4') ? 'fail' : 'error';
this.isOperational = true;
Error.captureStackTrace(this, this.constructor);
}
}
EOL
# Create catchAsync.js in utils folder
cat <<EOL > utils/catchAsync.js
const catchAsync = (fn) => {
return (req, res, next) => {
fn(req, res, next).catch(next);
};
};
export default catchAsync;
EOL
# Create connectDB.js in config folder
cat <<EOL > config/connectDB.js
import mongoose from 'mongoose';
import { MONGO_URI } from './constants.js';
const config = {
isConnected: 0,
};
const connectDB = async () => {
// Check if already connected to DB
if (config.isConnected) {
return;
}
const options = {
dbName: "$projectName",
};
try {
const { connection } = await mongoose.connect(MONGO_URI, options);
config.isConnected = connection.readyState;
console.log('✔️ Connected to DB 👍');
console.log('↗️ HOST:', connection.host);
console.log('↗️ HOST_NAME :', connection.name);
console.log('↗️ DB_PORT :', connection.port);
} catch (error) {
console.log('Failed to connect DB 💀💀💀');
console.error(error.message);
throw new Error(error);
}
};
export default connectDB;
EOL
# Create connectDB.js in config folder
cat <<EOL > config/constants.js
import dotenv from 'dotenv';
dotenv.config()
export const APP_URL = process.env.APP_URL;
export const PORT = process.env.PORT;
export const MONGO_URI = process.env.MONGO_URI;
export const JWT_SECRET = process.env.JWT_SECRET;
export const JWT_EXPIRES_IN = process.env.JWT_EXPIRES_IN;
EOL
# Create README.md
cat <<EOL > README.md
# Express MVC App
## Description
This is an Express.js application following the MVC (Model-View-Controller) architecture. It includes features such as JWT-based authentication, error handling, rate limiting, CORS, and more.
## Features
- JWT Authentication with session invalidation on multiple device logins
- Environment variable management via dotenv
- Express middleware for security headers, rate limiting, logging, etc.
- MongoDB connection management using Mongoose
- Structured app in MVC pattern
## How to run
1. Clone the repository.
2. Install dependencies using \`npm install\`.
3. Configure environment variables in the \`.env\` file.
4. Run the app using \`npm start\`.
EOL
# Output success message
echo "Project setup complete in $projectName!"