Skip to content

Commit 5b6e798

Browse files
committed
migration
1 parent 73a928a commit 5b6e798

File tree

11 files changed

+284
-0
lines changed

11 files changed

+284
-0
lines changed

server/.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

server/.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10

server/.prettierrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"semi": true,
3+
"trailingComma": "all",
4+
"bracketSpacing": true,
5+
"jsxBracketSameLine": false,
6+
"singleQuote": true,
7+
"overrides": [],
8+
"printWidth": 80,
9+
"useTabs": false,
10+
"tabWidth": 2,
11+
"parser": "typescript"
12+
}

server/jest.config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { jsWithBabel: tsjPreset } = require('ts-jest/presets');
2+
3+
module.exports = {
4+
preset: 'ts-jest',
5+
transform: { ...tsjPreset.transform },
6+
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
7+
moduleFileExtensions: ['ts', 'tsx', 'json', 'node'],
8+
globals: {
9+
'ts-jest': {
10+
babelConfig: false,
11+
},
12+
},
13+
testEnvironment: 'node',
14+
verbose: true,
15+
coverageDirectory: './coverage/',
16+
collectCoverage: true,
17+
};

server/package.json

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{
2+
"name": "react-bootstrap-webpack-starter-server",
3+
"version": "1.0.0",
4+
"author": "Erwan DATIN <erwan.datin@gmail.com> (MacKentoch)",
5+
"license": "MIT",
6+
"description": "server side of react js + bootstrap + webpack starter",
7+
"main": "out/index.js",
8+
"repository": {
9+
"type": "git",
10+
"url": "git+https://github.com/MacKentoch/react-bootstrap-webpack-starter.git"
11+
},
12+
"bugs": {
13+
"url": "https://github.com/MacKentoch/react-bootstrap-webpack-starter/issues"
14+
},
15+
"engines": {
16+
"node": ">=10",
17+
"npm": ">=6.0.0",
18+
"yarn": ">=1.3.0"
19+
},
20+
"directories": {
21+
"test": "test"
22+
},
23+
"scripts": {
24+
"test": "cross-env NODE_ENV=test jest",
25+
"build-server": "tsc src/index.ts --outDir out",
26+
"run-server": "npm run build-server && ts-node src/index.ts"
27+
},
28+
"keywords": [
29+
"node",
30+
"TS",
31+
"express",
32+
"react",
33+
"react 16",
34+
"redux",
35+
"react-redux",
36+
"ES6",
37+
"ES7",
38+
"ES2015",
39+
"ES2016",
40+
"ES2017",
41+
"ES2018",
42+
"ES2019",
43+
"typescript",
44+
"bootstrap",
45+
"react-router4",
46+
"react-router",
47+
"starter",
48+
"webpack",
49+
"hot-reload",
50+
"redux-devtools-extension",
51+
"devtools",
52+
"webpack4"
53+
],
54+
"dependencies": {
55+
"body-parser": "^1.19.0",
56+
"chalk": "^2.4.2",
57+
"compression": "^1.7.4",
58+
"convict": "^5.1.0",
59+
"date-fns": "^2.4.1",
60+
"express": "^4.17.1",
61+
"express-promise-router": "^3.0.3",
62+
"express-rate-limit": "^5.0.0",
63+
"pretty-error": "^2.1.1",
64+
"serialize-javascript": "^2.1.0",
65+
"serve-favicon": "^2.5.0"
66+
},
67+
"devDependencies": {
68+
"@types/body-parser": "^1.17.1",
69+
"@types/chalk": "^2.2.0",
70+
"@types/convict": "^4.2.1",
71+
"@types/express": "^4.17.1",
72+
"@types/express-promise-router": "^2.0.1",
73+
"@types/express-rate-limit": "^3.3.3",
74+
"@types/fetch-mock": "^7.3.1",
75+
"@types/jest": "^24.0.18",
76+
"@types/node": "^12.7.12",
77+
"fetch-mock": "^7.5.1",
78+
"jest": "^24.9.0",
79+
"jest-localstorage-mock": "^2.4.0",
80+
"prettier": "^1.18.2",
81+
"rimraf": "^3.0.0",
82+
"ts-jest": "^24.1.0",
83+
"ts-node": "^8.4.1",
84+
"tslib": "^1.10.0",
85+
"tslint-config-prettier": "^1.18.0",
86+
"typescript": "^3.6.4"
87+
}
88+
}

server/src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as express from 'express';
2+
import * as PrettyError from 'pretty-error';
3+
import expressServer from './lib/expressServer';
4+
5+
// #region constants
6+
const dev = process.env.NODE_ENV !== 'production';
7+
const pe = new PrettyError();
8+
// #endregion
9+
10+
(() => {
11+
try {
12+
pe.start();
13+
const app = express();
14+
expressServer(app, dev);
15+
} catch (error) {
16+
console.log('server error: ', error);
17+
}
18+
})();

server/src/lib/asyncWrap.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as express from 'express';
2+
3+
// #region constants
4+
const shouldLogErrors = process.env.DEBUG || false;
5+
// #endregion
6+
7+
/**
8+
* Returns a route handler for Express that calls the passed in function
9+
*/
10+
export default function(fn: Function) {
11+
if (fn.length <= 3) {
12+
return function(
13+
req: express.Request,
14+
res: express.Response,
15+
next: express.NextFunction,
16+
) {
17+
return fn(req, res, next).catch((error: any) => {
18+
if (shouldLogErrors) {
19+
console.log('middleware error: ', error);
20+
}
21+
next();
22+
});
23+
};
24+
} else {
25+
return function(
26+
err: express.Errback,
27+
req: express.Request,
28+
res: express.Response,
29+
next: express.NextFunction,
30+
) {
31+
return fn(err, req, res, next).catch((error: any) => {
32+
if (shouldLogErrors) {
33+
console.log('middleware error: ', error);
34+
}
35+
next();
36+
});
37+
};
38+
}
39+
}

server/src/lib/expressServer.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as express from 'express';
2+
import * as path from 'path';
3+
import chalk from 'chalk';
4+
import { error404, error500 } from '../middleware/errors';
5+
6+
// #region constants
7+
const DOCS_PATH = '../../../docs/';
8+
const port = process.env.PORT || 8082;
9+
const host = process.env.SERVER_HOST || 'localhost';
10+
// #endregion
11+
12+
const expressServer = (app: express.Application, isDev = false) => {
13+
if (!app) {
14+
console.log('Server application instance is undefined');
15+
throw new Error('Server application instance is undefined');
16+
}
17+
18+
app.set('port', port);
19+
app.set('ipAdress', host);
20+
21+
app.use(
22+
'/assets',
23+
express.static(path.join(__dirname, DOCS_PATH, 'assets/')),
24+
);
25+
26+
app.get('/*', (req, res) =>
27+
res.sendFile(path.join(__dirname, DOCS_PATH, 'index.html')),
28+
);
29+
30+
app.use(error404);
31+
app.use(error500);
32+
33+
/* eslint-disable no-console */
34+
// @ts-ignore
35+
app.listen(port, host, () =>
36+
console.log(`
37+
=====================================================
38+
-> Server (${chalk.bgBlue('SPA')}) 🏃 (running) on ${chalk.green(
39+
host,
40+
)}:${chalk.green(`${port}`)}
41+
=====================================================
42+
`),
43+
);
44+
/* eslint-enable no-console */
45+
46+
return app;
47+
};
48+
49+
export default expressServer;

server/src/middleware/errors.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as express from 'express';
2+
3+
type ErrorWithStatus = {
4+
status?: number,
5+
message?: string,
6+
};
7+
8+
export const error404 = (
9+
req: express.Request,
10+
res: express.Response,
11+
next: express.NextFunction,
12+
) => {
13+
console.log('req.url: ', req.url);
14+
15+
const err: ErrorWithStatus = new Error('Not found');
16+
err.status = 404;
17+
next(err);
18+
};
19+
20+
export const error500 = (
21+
err: express.Errback & ErrorWithStatus,
22+
req: express.Request,
23+
res: express.Response,
24+
next: express.NextFunction,
25+
) => {
26+
if (err.status === 404) {
27+
res.status(404).send('Sorry nothing here for now...');
28+
}
29+
30+
console.error(err);
31+
res.status(500).send('internal server error');
32+
};

server/test/setupTests.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('jest-localstorage-mock');

server/tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"noImplicitReturns": true,
5+
"noUnusedLocals": true,
6+
"sourceMap": true,
7+
"strict": true,
8+
"target": "es2017",
9+
"resolveJsonModule": true,
10+
"moduleResolution": "node",
11+
"baseUrl": ".",
12+
"outDir": "out"
13+
},
14+
"include": ["./src/**/*", "./test/**/*"]
15+
}

0 commit comments

Comments
 (0)