-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
79 lines (64 loc) · 2.1 KB
/
server.js
File metadata and controls
79 lines (64 loc) · 2.1 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
70
71
72
73
74
75
76
77
78
79
import 'dotenv/config'
import express from 'express'
import cors from 'cors'
import { createServer } from 'http'
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const app = express()
const server = createServer(app)
// Middleware
app.use(cors())
app.use(express.json())
app.use(express.static('.'))
// Basic route
app.get('/', (req, res) => {
res.send('Server is running')
})
app.get('/favicon.ico', (req, res) => {
res.status(204).end()
})
// MongoDB and game logic are optional - comment out if not needed
import setupGameLogic from './gameLogic.js'
import connectDB from './config/db.js'
const initializeServer = async () => {
if (process.env.NODE_ENV === 'test') return
await connectDB()
setupGameLogic(server)
}
const startServer = async () => {
await initializeServer()
const PORT = process.env.PORT || 3000
await new Promise((resolve, reject) => {
server.once('error', reject)
server.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`)
console.log(`Solo game: http://localhost:${PORT}/solo/index.html`)
resolve()
})
})
return server
}
const isDirectRun = process.argv[1] && path.resolve(process.argv[1]) === __filename
if (isDirectRun) {
startServer().catch(err => {
console.error('Failed to start server:', err)
process.exit(1)
})
}
// Dev logging endpoint for client-side test hooks
app.post('/_dev/log', (req, res) => {
try {
const payload = req.body || {}
// Only print dev logs when explicitly allowed (reduce noise).
const tag = String(payload.tag || '')
const shouldLog = process.env.NODE_ENV === 'development' || process.env.DEV_LOG === '1' || tag.startsWith('test-') || tag.includes('craft') || tag.startsWith('dev')
if (shouldLog) console.log('[DEV LOG]', payload.tag || 'client', payload.message || payload)
res.status(200).json({ ok: true })
} catch (err) {
console.error('Failed to process /_dev/log:', err)
res.status(500).json({ ok: false })
}
})
export { app, server, startServer }