-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
69 lines (60 loc) · 2.06 KB
/
Copy pathserver.js
File metadata and controls
69 lines (60 loc) · 2.06 KB
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
const express = require('express');
const path = require('path');
const fs = require('fs');
const apiRoutes = require('./src/routes/api');
const puppeteer = require('puppeteer');
// Load environment variables: prefer external .env near installed app; fallback to embedded .env
try {
const isElectron = !!(process.versions && process.versions.electron);
const baseDir = isElectron ? (process.resourcesPath || __dirname) : __dirname;
const externalEnv = isElectron ? path.join(process.cwd(), '.env') : path.join(__dirname, '.env');
const embeddedEnv = path.join(baseDir, '.env');
const dotenv = require('dotenv');
if (fs.existsSync(externalEnv)) {
dotenv.config({ path: externalEnv });
} else if (fs.existsSync(embeddedEnv)) {
dotenv.config({ path: embeddedEnv });
} else {
dotenv.config(); // default fallback
}
} catch (_) {}
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(express.json()); // for parsing application/json
app.use(express.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
// View engine setup
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Static files
app.use('/static', express.static(path.join(__dirname, 'public')));
// Routes
app.get('/', (req, res) => {
res.render('index');
});
app.use('/api', apiRoutes);
// Browser launch function
async function launchBrowser() {
const launchOptions = {
headless: true,
// ...other options...
};
if (process.env.CHROME_EXECUTABLE_PATH) {
launchOptions.executablePath = process.env.CHROME_EXECUTABLE_PATH;
}
const browser = await puppeteer.launch(launchOptions);
return browser;
}
// Start server
app.listen(PORT, () => {
try {
const isElectron = !!(process.versions && process.versions.electron);
if (!isElectron && process.platform === 'win32') {
const { exec } = require('child_process');
exec(`start "" "http://localhost:${PORT}"`);
}
} catch (_) {
// ignore failures
}
console.log(`🚀 Server is running at http://localhost:${PORT}`);
});