-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (74 loc) · 1.87 KB
/
index.js
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
const express = require('express');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const pm2 = require('pm2');
const app = express();
const port = 3000;
app.set('view engine', 'ejs');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
// Set up multer for handling file uploads
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/uploads/');
},
filename: function (req, file, cb) {
cb(null, Date.now() + path.extname(file.originalname));
}
});
const upload = multer({ storage: storage });
app.get('/', function(req, res) {
res.send('hello');
});
app.get('/upload', function(req, res) {
const uploadedImages = fs.readdirSync('public/uploads/');
res.render('uploadImages', { images: uploadedImages });
});
app.post('/upload', upload.array('images', 5), function(req, res) {
res.redirect('/upload');
});
// PM2 process management
function startPM2() {
pm2.connect(function(err) {
if (err) {
console.error(err);
process.exit(2);
}
pm2.start({
script: 'index.js',
name: 'image-upload-app',
exec_mode: 'cluster',
instances: 2
}, function(err, apps) {
if (err) {
console.error(err);
return pm2.disconnect();
}
console.log('PM2 started');
pm2.disconnect();
});
});
}
if (require.main === module) {
// Start the Express server
app.listen(port, function() {
console.log('Server is running on port 3000');
});
// Start PM2
startPM2();
} else {
// Export for PM2
module.exports = app;
}
process.on('message', function(packet) {
console.log('Received message:', packet);
// You can handle the received message here
process.send({
type: 'process:msg',
data: {
success: true
}
});
});