-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
36 lines (30 loc) · 1.45 KB
/
Copy pathserver.js
File metadata and controls
36 lines (30 loc) · 1.45 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
/**
* Custom Next.js server using api-ape library without Express
*/
const { createServer } = require('http')
const next = require('next')
const { ape } = require('api-ape')
const { onConnect } = require('./ape/onConnect')
const dev = process.env.NODE_ENV !== 'production'
const hostname = '0.0.0.0'
const port = parseInt(process.env.PORT, 10) || 3000
const app = next({ dev, hostname, port })
const handle = app.getRequestHandler()
app.prepare().then(() => {
const server = createServer((req, res) => {
return handle(req, res)
})
// Initialize api-ape with the raw http server
ape(server, { where: 'api', onConnect })
server.listen(port, () => {
console.log(`
╔═══════════════════════════════════════════════════════╗
║ api-ape NextJS Demo ║
╠═══════════════════════════════════════════════════════╣
║ HTTP: http://localhost:${port}/ ║
║ WebSocket: ws://localhost:${port}/api/ape ║
║ ape(server, { where: "api", onConnect }) ║
╚═══════════════════════════════════════════════════════╝
`)
})
})