Skip to content

Commit ba65b07

Browse files
brikoutimneutkens
authored andcommitted
Ease running multiple examples at the same time with process.env.PORT (vercel#2753)
1 parent d0b95d9 commit ba65b07

File tree

23 files changed

+64
-44
lines changed

23 files changed

+64
-44
lines changed

examples/custom-server-express/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require('express')
22
const next = require('next')
33

4+
const port = parseInt(process.env.PORT, 10) || 3000
45
const dev = process.env.NODE_ENV !== 'production'
56
const app = next({ dev })
67
const handle = app.getRequestHandler()
@@ -21,8 +22,8 @@ app.prepare()
2122
return handle(req, res)
2223
})
2324

24-
server.listen(3000, (err) => {
25+
server.listen(port, (err) => {
2526
if (err) throw err
26-
console.log('> Ready on http://localhost:3000')
27+
console.log(`> Ready on http://localhost:${port}`)
2728
})
2829
})

examples/custom-server-hapi/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Hapi = require('hapi')
33
const Good = require('good')
44
const { pathWrapper, defaultHandlerWrapper } = require('./next-wrapper')
55

6+
const port = parseInt(process.env.PORT, 10) || 3000
67
const dev = process.env.NODE_ENV !== 'production'
78
const app = next({ dev })
89
const server = new Hapi.Server()
@@ -23,7 +24,7 @@ const pluginOptions = [
2324

2425
app.prepare()
2526
.then(() => {
26-
server.connection({ port: 3000 })
27+
server.connection({ port })
2728
server.register(pluginOptions)
2829
.then(() => {
2930
server.route({
@@ -48,7 +49,7 @@ app.prepare()
4849
console.log('Error starting server')
4950
console.log(error)
5051
}).then(() => {
51-
console.log('> Ready on http://localhost:3000')
52+
console.log(`> Ready on http://localhost:${port}`)
5253
})
5354
})
5455
})

examples/custom-server-koa/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const Koa = require('koa')
22
const next = require('next')
33
const Router = require('koa-router')
44

5+
const port = parseInt(process.env.PORT, 10) || 3000
56
const dev = process.env.NODE_ENV !== 'production'
67
const app = next({ dev })
78
const handle = app.getRequestHandler()
@@ -32,8 +33,8 @@ app.prepare()
3233
})
3334

3435
server.use(router.routes())
35-
server.listen(3000, (err) => {
36+
server.listen(port, (err) => {
3637
if (err) throw err
37-
console.log('> Ready on http://localhost:3000')
38+
console.log(`> Ready on http://localhost:${port}`)
3839
})
3940
})

examples/custom-server-micro/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const match = require('micro-route/match')
33
const { parse } = require('url')
44
const next = require('next')
55

6+
const port = parseInt(process.env.PORT, 10) || 3000
67
const dev = process.env.NODE_ENV !== 'production'
78
const app = next({ dev })
89
const handle = app.getRequestHandler()
@@ -21,8 +22,8 @@ const server = micro(async (req, res) => {
2122
})
2223

2324
app.prepare().then(() => {
24-
server.listen(3000, err => {
25+
server.listen(port, err => {
2526
if (err) throw err
26-
console.log('> Ready on http://localhost:3000')
27+
console.log(`> Ready on http://localhost:${port}`)
2728
})
2829
})

examples/custom-server/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const { createServer } = require('http')
22
const { parse } = require('url')
33
const next = require('next')
44

5+
const port = parseInt(process.env.PORT, 10) || 3000
56
const dev = process.env.NODE_ENV !== 'production'
67
const app = next({ dev })
78
const handle = app.getRequestHandler()
@@ -20,8 +21,8 @@ app.prepare()
2021
handle(req, res, parsedUrl)
2122
}
2223
})
23-
.listen(3000, (err) => {
24+
.listen(port, (err) => {
2425
if (err) throw err
25-
console.log('> Ready on http://localhost:3000')
26+
console.log(`> Ready on http://localhost:${port}`)
2627
})
2728
})

examples/page-transitions/server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const routes = require('./routes')
33
const express = require('express')
44
const compression = require('compression')
55

6-
const port = process.env.PORT || 3000
6+
const port = parseInt(process.env.PORT, 10) || 3000
77
const dev = process.env.NODE_ENV !== 'production'
88

99
const app = next({ dev })

examples/parameterized-routing/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { parse } = require('url')
33
const next = require('next')
44
const pathMatch = require('path-match')
55

6+
const port = parseInt(process.env.PORT, 10) || 3000
67
const dev = process.env.NODE_ENV !== 'production'
78
const app = next({ dev })
89
const handle = app.getRequestHandler()
@@ -23,8 +24,8 @@ app.prepare()
2324
// i.e. /blog/foo?show-comments=true
2425
app.render(req, res, '/blog', Object.assign(params, query))
2526
})
26-
.listen(3000, (err) => {
27+
.listen(port, (err) => {
2728
if (err) throw err
28-
console.log('> Ready on http://localhost:3000')
29+
console.log(`> Ready on http://localhost:${port}`)
2930
})
3031
})

examples/root-static-files/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { parse } = require('url')
33
const next = require('next')
44
const { join } = require('path')
55

6+
const port = parseInt(process.env.PORT, 10) || 3000
67
const dev = process.env.NODE_ENV !== 'production'
78
const app = next({ dev })
89
const handle = app.getRequestHandler()
@@ -23,8 +24,8 @@ app.prepare()
2324
handle(req, res, parsedUrl)
2425
}
2526
})
26-
.listen(3000, (err) => {
27+
.listen(port, (err) => {
2728
if (err) throw err
28-
console.log('> Ready on http://localhost:3000')
29+
console.log(`> Ready on http://localhost:${port}`)
2930
})
3031
})

examples/ssr-caching/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const express = require('express')
22
const next = require('next')
33
const LRUCache = require('lru-cache')
44

5+
const port = parseInt(process.env.PORT, 10) || 3000
56
const dev = process.env.NODE_ENV !== 'production'
67
const app = next({ dir: '.', dev })
78
const handle = app.getRequestHandler()
@@ -30,9 +31,9 @@ app.prepare()
3031
return handle(req, res)
3132
})
3233

33-
server.listen(3000, (err) => {
34+
server.listen(port, (err) => {
3435
if (err) throw err
35-
console.log('> Ready on http://localhost:3000')
36+
console.log(`> Ready on http://localhost:${port}`)
3637
})
3738
})
3839

examples/using-inferno/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const port = parseInt(process.env.PORT, 10) || 3000
12
const dev = process.env.NODE_ENV !== 'production'
23
const moduleAlias = require('module-alias')
34

@@ -22,8 +23,8 @@ app.prepare()
2223
const parsedUrl = parse(req.url, true)
2324
handle(req, res, parsedUrl)
2425
})
25-
.listen(3000, (err) => {
26+
.listen(port, (err) => {
2627
if (err) throw err
27-
console.log('> Ready on http://localhost:3000')
28+
console.log(`> Ready on http://localhost:${port}`)
2829
})
2930
})

examples/using-preact/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const port = parseInt(process.env.PORT, 10) || 3000
12
const dev = process.env.NODE_ENV !== 'production'
23
const moduleAlias = require('module-alias')
34

@@ -21,8 +22,8 @@ app.prepare()
2122
const parsedUrl = parse(req.url, true)
2223
handle(req, res, parsedUrl)
2324
})
24-
.listen(3000, (err) => {
25+
.listen(port, (err) => {
2526
if (err) throw err
26-
console.log('> Ready on http://localhost:3000')
27+
console.log(`> Ready on http://localhost:${port}`)
2728
})
2829
})

examples/with-custom-reverse-proxy/server.es6.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const devProxy = {
1010
}
1111
}
1212

13-
const port = process.env.PORT || 3000
13+
const port = parseInt(process.env.PORT, 10) || 3000
1414
const env = process.env.NODE_ENV
1515
const dev = env !== 'production'
1616
const app = next({

examples/with-firebase-authentication/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const FileStore = require('session-file-store')(session)
55
const next = require('next')
66
const admin = require('firebase-admin')
77

8+
const port = parseInt(process.env.PORT, 10) || 3000
89
const dev = process.env.NODE_ENV !== 'production'
910
const app = next({ dev })
1011
const handle = app.getRequestHandler()
@@ -56,8 +57,8 @@ app.prepare()
5657
return handle(req, res)
5758
})
5859

59-
server.listen(3000, (err) => {
60+
server.listen(port, (err) => {
6061
if (err) throw err
61-
console.log('> Ready on http://localhost:3000')
62+
console.log(`> Ready on http://localhost:${port}`)
6263
})
6364
})

examples/with-mobx/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const port = parseInt(process.env.PORT, 10) || 3000
12
const dev = process.env.NODE_ENV !== 'production'
23

34
const { createServer } = require('http')
@@ -13,8 +14,8 @@ app.prepare().then(() => {
1314
createServer((req, res) => {
1415
const parsedUrl = parse(req.url, true)
1516
handle(req, res, parsedUrl)
16-
}).listen(3000, err => {
17+
}).listen(port, err => {
1718
if (err) throw err
18-
console.log('> Ready on http://localhost:3000')
19+
console.log(`> Ready on http://localhost:${port}`)
1920
})
2021
})

examples/with-next-routes/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ const { createServer } = require('http')
22
const next = require('next')
33
const routes = require('./routes')
44

5+
const port = parseInt(process.env.PORT, 10) || 3000
56
const dev = process.env.NODE_ENV !== 'production'
67
const app = next({ dev })
78
const handler = routes.getRequestHandler(app)
89

910
app.prepare()
1011
.then(() => {
1112
createServer(handler)
12-
.listen(3000, (err) => {
13+
.listen(port, (err) => {
1314
if (err) throw err
14-
console.log('> Ready on http://localhost:3000')
15+
console.log(`> Ready on http://localhost:${port}`)
1516
})
1617
})

examples/with-pkg/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ const { createServer } = require('http')
22
const { parse } = require('url')
33
const next = require('next')
44

5+
const port = parseInt(process.env.PORT, 10) || 3000
56
const dev = process.env.NODE_ENV !== 'production'
67
const app = next({ dev })
78
const handle = app.getRequestHandler()
89

910
app.prepare()
1011
.then(() => {
1112
createServer((req, res) => handle(req, res, parse(req.url, true).pathname))
12-
.listen(3000, (err) => {
13+
.listen(port, (err) => {
1314
if (err) throw err
14-
console.log('> Ready on http://localhost:3000')
15+
console.log(`> Ready on http://localhost:${port}`)
1516
})
1617
})

examples/with-pretty-url-routing/server.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ const express = require('express')
22
const next = require('next')
33
const Router = require('./routes').Router
44

5-
const dev = process.env.NODE_ENV !== 'production'
65
const port = parseInt(process.env.PORT, 10) || 3000
7-
const app = next({dev})
6+
const dev = process.env.NODE_ENV !== 'production'
7+
const app = next({ dev })
88
const handle = app.getRequestHandler()
99

1010
app.prepare()

examples/with-react-i18next/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const express = require('express')
22
const path = require('path')
33
const next = require('next')
44

5+
const port = parseInt(process.env.PORT, 10) || 3000
56
const dev = process.env.NODE_ENV !== 'production'
67
const app = next({ dev })
78
const handle = app.getRequestHandler()
@@ -40,9 +41,9 @@ i18n
4041
// use next.js
4142
server.get('*', (req, res) => handle(req, res))
4243

43-
server.listen(3000, (err) => {
44+
server.listen(port, (err) => {
4445
if (err) throw err
45-
console.log('> Ready on http://localhost:3000')
46+
console.log(`> Ready on http://localhost:${port}`)
4647
})
4748
})
4849
})

examples/with-react-intl/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const accepts = require('accepts')
1111
const glob = require('glob')
1212
const next = require('next')
1313

14+
const port = parseInt(process.env.PORT, 10) || 3000
1415
const dev = process.env.NODE_ENV !== 'production'
1516
const app = next({dev})
1617
const handle = app.getRequestHandler()
@@ -46,8 +47,8 @@ app.prepare().then(() => {
4647
req.localeDataScript = getLocaleDataScript(locale)
4748
req.messages = dev ? {} : getMessages(locale)
4849
handle(req, res)
49-
}).listen(3000, (err) => {
50+
}).listen(port, (err) => {
5051
if (err) throw err
51-
console.log('> Read on http://localhost:3000')
52+
console.log(`> Ready on http://localhost:${port}`)
5253
})
5354
})

examples/with-socket.io/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const server = require('http').Server(app)
33
const io = require('socket.io')(server)
44
const next = require('next')
55

6+
const port = parseInt(process.env.PORT, 10) || 3000
67
const dev = process.env.NODE_ENV !== 'production'
78
const nextApp = next({ dev })
89
const nextHandler = nextApp.getRequestHandler()
@@ -27,8 +28,8 @@ nextApp.prepare().then(() => {
2728
return nextHandler(req, res)
2829
})
2930

30-
server.listen(3000, (err) => {
31+
server.listen(port, (err) => {
3132
if (err) throw err
32-
console.log('> Ready on http://localhost:3000')
33+
console.log(`> Ready on http://localhost:${port}`)
3334
})
3435
})

examples/with-static-export/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require('express')
22
const next = require('next')
33

4+
const port = parseInt(process.env.PORT, 10) || 3000
45
const dev = process.env.NODE_ENV !== 'production'
56
const app = next({ dev })
67
const handle = app.getRequestHandler()
@@ -20,8 +21,8 @@ app.prepare()
2021
return handle(req, res)
2122
})
2223

23-
server.listen(3000, (err) => {
24+
server.listen(port, (err) => {
2425
if (err) throw err
25-
console.log('> Ready on http://localhost:3000')
26+
console.log(`> Ready on http://localhost:${port}`)
2627
})
2728
})

examples/with-sw-precache/server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { join } = require('path')
33
const { parse } = require('url')
44
const next = require('next')
55

6+
const port = parseInt(process.env.PORT, 10) || 3000
67
const dev = process.env.NODE_ENV !== 'production'
78
const app = next({ dev })
89
const handle = app.getRequestHandler()
@@ -20,8 +21,8 @@ app.prepare()
2021
handle(req, res, parsedUrl)
2122
}
2223
})
23-
.listen(3000, (err) => {
24+
.listen(port, (err) => {
2425
if (err) throw err
25-
console.log('> Ready on http://localhost:3000')
26+
console.log(`> Ready on http://localhost:${port}`)
2627
})
2728
})

0 commit comments

Comments
 (0)