Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Mar 9, 2021. It is now read-only.

Commit 4839c24

Browse files
committed
configure get-port npm package
1 parent 248340e commit 4839c24

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

package.json

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "xo && jest",
8-
"lint":
9-
"prettier 'utils/**/*.js' 'components/**/*.js' 'pages/**/*.js' 'lib/**/*.js' 'hocs/**/*.js' '*.js' --write && xo",
8+
"lint": "prettier 'utils/**/*.js' 'components/**/*.js' 'pages/**/*.js' 'lib/**/*.js' 'hocs/**/*.js' '*.js' --write && xo",
109
"precommit": "lint-staged",
1110
"analyze": "cross-env ANALYZE=1 next build",
1211
"dev": "cross-env NODE_ENV=development node server.js",
@@ -16,19 +15,32 @@
1615
},
1716
"xo": {
1817
"parser": "babel-eslint",
19-
"extends": ["prettier", "prettier/react", "plugin:react/recommended"],
20-
"env": ["browser", "node"],
18+
"extends": [
19+
"prettier",
20+
"prettier/react",
21+
"plugin:react/recommended"
22+
],
23+
"env": [
24+
"browser",
25+
"node"
26+
],
2127
"rules": {
2228
"linebreak-style": 0,
2329
"react/display-name": 0,
2430
"react/prop-types": 0
2531
},
2632
"space:": 2,
27-
"ignores": ["next.config.js"],
33+
"ignores": [
34+
"next.config.js"
35+
],
2836
"overrides": [
2937
{
3038
"files": "**/__tests__/*.test.js",
31-
"globals": ["describe", "it", "expect"]
39+
"globals": [
40+
"describe",
41+
"it",
42+
"expect"
43+
]
3244
}
3345
]
3446
},
@@ -45,6 +57,7 @@
4557
"date-fns": "^1.29.0",
4658
"emotion": "^8.0.10",
4759
"emotion-server": "^8.0.10",
60+
"get-port": "^3.2.0",
4861
"grid-emotion": "^2.1.0",
4962
"isomorphic-unfetch": "2.0.0",
5063
"lodash.take": "^4.1.1",

server.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,31 @@ const { parse } = require('url');
33
const next = require('next');
44
const pathMatch = require('path-match');
55
const opn = require('opn');
6+
const getPort = require('get-port');
67

7-
const port = parseInt(process.env.PORT, 10) || 3000;
88
const dev = process.env.NODE_ENV !== 'production';
99
const app = next({ dev });
1010
const handle = app.getRequestHandler();
1111
const route = pathMatch();
1212
const match = route('/learn/:id');
1313

14-
app.prepare().then(() => {
15-
createServer((req, res) => {
16-
const { pathname, query } = parse(req.url, true);
17-
const params = match(pathname);
18-
if (params === false) {
19-
handle(req, res);
20-
return;
21-
}
22-
// Assigning `query` into the params means that we still
23-
// get the query string passed to our application
24-
// i.e. /blog/foo?show-comments=true
25-
app.render(req, res, '/learn/subject', Object.assign(params, query));
26-
}).listen(port, err => {
27-
if (err) throw err;
28-
console.log(`>> App running on http://localhost:${port}`);
29-
opn(`http://localhost:${port}`);
14+
getPort({ port: 3000 }).then(port => {
15+
app.prepare().then(() => {
16+
createServer((req, res) => {
17+
const { pathname, query } = parse(req.url, true);
18+
const params = match(pathname);
19+
if (params === false) {
20+
handle(req, res);
21+
return;
22+
}
23+
// Assigning `query` into the params means that we still
24+
// get the query string passed to our application
25+
// i.e. /blog/foo?show-comments=true
26+
app.render(req, res, '/learn/subject', Object.assign(params, query));
27+
}).listen(port, err => {
28+
if (err) throw err;
29+
console.log(`>> App running on http://localhost:${port}`);
30+
opn(`http://localhost:${port}`);
31+
});
3032
});
3133
});

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2912,6 +2912,10 @@ get-pkg-repo@^1.0.0:
29122912
parse-github-repo-url "^1.3.0"
29132913
through2 "^2.0.0"
29142914

2915+
get-port@^3.2.0:
2916+
version "3.2.0"
2917+
resolved "https://registry.yarnpkg.com/get-port/-/get-port-3.2.0.tgz#dd7ce7de187c06c8bf353796ac71e099f0980ebc"
2918+
29152919
get-set-props@^0.1.0:
29162920
version "0.1.0"
29172921
resolved "https://registry.yarnpkg.com/get-set-props/-/get-set-props-0.1.0.tgz#998475c178445686d0b32246da5df8dbcfbe8ea3"

0 commit comments

Comments
 (0)