Skip to content

Commit eaa0c81

Browse files
committed
Include base eslint configuration for backend projects.
1 parent 079a1ab commit eaa0c81

39 files changed

+613
-463
lines changed

.eslintrc.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
browser: true,
5+
commonjs: true,
6+
es2021: true
7+
},
8+
overrides: [
9+
{
10+
env: {
11+
node: true
12+
},
13+
files: [
14+
'.eslintrc.{js,cjs}'
15+
],
16+
parserOptions: {
17+
sourceType: 'script'
18+
}
19+
}
20+
],
21+
parserOptions: {
22+
ecmaVersion: 'latest'
23+
},
24+
rules: {
25+
semi: [2, 'always'],
26+
"no-unused-vars": ["error", { "vars": "all", "args": "none" }],
27+
quotes: ["error", "single", { "avoidEscape": true }],
28+
"space-before-function-paren": ["error", "never"]
29+
}
30+
}

.pre-commit-config.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,18 @@ repos:
3232
# Spell checks files, ignoring specific words if needed
3333
- id: codespell
3434
args: ['--ignore-words-list=seperated']
35+
36+
- repo: https://github.com/pre-commit/mirrors-eslint
37+
rev: v8.52.0
38+
hooks:
39+
- id: eslint
40+
exclude: 'frontend/.*|.eslintrc.js'
41+
files: \.[jt]sx?$ # *.js, *.jsx, *.ts and *.tsx
42+
types: [file]
43+
additional_dependencies:
44+
- eslint@8.52.0
45+
- eslint-config-standard@17.1.0
46+
- eslint-plugin-import@2.29.0
47+
- eslint-plugin-n@16.2.0
48+
- eslint-plugin-promise@6.1.1
49+
- eslint-config-next@13.5.5

backend/node-js-express/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
# Node-Express Seeds (JavaScript Version)
22

33
This repository contains multiple seeds (starter templates) for Node.js projects using Express.js with different
4-
configurations and additions.
4+
configurations and features as well as persistence options.
55

66
## Folder Structure
77

88
- **node-js-express-starter**: This contains a basic Node-Express seed to get you started with a simple "Hello, World!"
99
API.
1010
- **node-js-express-mongoose**: This seed incorporates Mongoose, enabling you to quickly start a project with MongoDB
1111
integration.
12+
- **node-js-express-postgresql**: This seed incorporates Postgres, enabling you to quickly start a project with
13+
PostgreSQL integration.
14+
15+
## Code Style
16+
17+
The code style of these projects should be enforced by eslint to avoid messing up with your code editor configuration.
18+
Make sure to check the existing node-js seeds, to get an idea of the basic eslint configuration that you can extend.
1219

1320
## Getting Started
1421

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
commonjs: true,
5+
es2021: true
6+
},
7+
// remove the following line for local development
8+
extends: '../../../.eslintrc.js',
9+
overrides: [
10+
{
11+
env: {
12+
node: true
13+
},
14+
files: [
15+
'.eslintrc.{js,cjs}'
16+
],
17+
parserOptions: {
18+
sourceType: 'script'
19+
}
20+
}
21+
],
22+
parserOptions: {
23+
ecmaVersion: 'latest'
24+
}
25+
};

backend/node-js-express/node-js-express-mongoose/app/app.js

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,34 @@ const HealthController = require('./health/health.controller');
1010
const UsersController = require('./user/user.controller');
1111

1212
class App {
13-
static bootstrap() {
14-
return new App();
15-
}
16-
17-
constructor() {
18-
// create express js application
19-
this.app = express();
20-
21-
// Connect to MongoDB
22-
dbConfig.connect();
23-
24-
// add routes
25-
this.routes();
26-
}
27-
28-
dbConnect(){
29-
dbConfig.connect();
30-
}
31-
routes() {
32-
this.app.use('/', new RootController().getRouter());
33-
this.app.use('/healthz', new HealthController().getRouter());
34-
this.app.use(`/users`, new UsersController().getRouter());
35-
}
13+
static bootstrap() {
14+
return new App();
15+
}
16+
17+
constructor() {
18+
// create express js application
19+
this.app = express();
20+
21+
// Connect to MongoDB
22+
dbConfig.connect();
23+
24+
// add routes
25+
this.routes();
26+
}
27+
28+
dbConnect() {
29+
dbConfig.connect();
30+
}
31+
32+
routes() {
33+
this.app.use('/', new RootController().getRouter());
34+
this.app.use('/healthz', new HealthController().getRouter());
35+
this.app.use('/users', new UsersController().getRouter());
36+
}
3637
}
3738

3839
function expressApp() {
39-
return new App().app;
40+
return new App().app;
4041
}
4142

4243
module.exports = expressApp;
Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
const mongoose = require('mongoose');
22

33
const dbConfig = {
4-
connect: function() {
5-
const MONGO_URI = process.env.MONGO_URI || 'mongodb://localhost:27017/test'; // Default URI
4+
connect: function() {
5+
const MONGO_URI = process.env.MONGO_URI || 'mongodb://localhost:27017/test'; // Default URI
66

7-
mongoose.connect(MONGO_URI, {
8-
useUnifiedTopology: true,
9-
useNewUrlParser: true,
10-
})
11-
.then(() => {
12-
console.log('Successfully connected to MongoDB.');
13-
})
14-
.catch(error => {
15-
console.error('Error connecting to MongoDB:', error);
16-
process.exit(1);
17-
});
7+
mongoose.connect(MONGO_URI, {
8+
useUnifiedTopology: true,
9+
useNewUrlParser: true
10+
})
11+
.then(() => {
12+
console.log('Successfully connected to MongoDB.');
13+
})
14+
.catch(error => {
15+
console.error('Error connecting to MongoDB:', error);
16+
process.exit(1);
17+
});
1818

19-
// Setting up mongoose event handlers
20-
mongoose.connection.on('error', err => {
21-
console.error('MongoDB connection error:', err);
22-
});
19+
// Setting up mongoose event handlers
20+
mongoose.connection.on('error', err => {
21+
console.error('MongoDB connection error:', err);
22+
});
2323

24-
mongoose.connection.on('disconnected', () => {
25-
console.log('MongoDB disconnected.');
26-
});
24+
mongoose.connection.on('disconnected', () => {
25+
console.log('MongoDB disconnected.');
26+
});
2727

28-
process.on('SIGINT', async () => {
29-
try {
30-
await mongoose.connection.close();
31-
console.log('MongoDB connection closed due to app termination.');
32-
process.exit(0);
33-
} catch (err) {
34-
console.error('Error during database disconnection:', err);
35-
process.exit(1);
36-
}
37-
});
38-
}
28+
process.on('SIGINT', async() => {
29+
try {
30+
await mongoose.connection.close();
31+
console.log('MongoDB connection closed due to app termination.');
32+
process.exit(0);
33+
} catch (err) {
34+
console.error('Error during database disconnection:', err);
35+
process.exit(1);
36+
}
37+
});
38+
}
3939
};
4040

4141
module.exports = dbConfig;

backend/node-js-express/node-js-express-mongoose/app/health/health.controller.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ const express = require('express');
33
const HealthService = require('./health.service');
44

55
class HealthController {
6-
constructor() {
7-
this.router = express.Router();
6+
constructor() {
7+
this.router = express.Router();
88

9-
// Setting the routes for the greeting endpoint group
10-
this.router.get('/', this.health.bind(this)); // Binding is necessary for 'this' to refer to the class instance
11-
}
9+
// Setting the routes for the greeting endpoint group
10+
this.router.get('/', this.health.bind(this)); // Binding is necessary for 'this' to refer to the class instance
11+
}
1212

13-
getRouter() {
14-
return this.router;
15-
}
13+
getRouter() {
14+
return this.router;
15+
}
1616

17-
async health(request, response, next) {
18-
const message = HealthService.health();
19-
response.status(200).send(message);
20-
}
17+
async health(request, response, next) {
18+
const message = HealthService.health();
19+
response.status(200).send(message);
20+
}
2121
}
2222

2323
module.exports = HealthController;
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class HealthService {
2-
health() {
3-
return "I am healthy!";
4-
}
2+
health() {
3+
return 'I am healthy!';
4+
}
55
}
66

7-
module.exports = new HealthService(); // Exporting an instance of the service
7+
module.exports = new HealthService(); // Exporting an instance of the service

backend/node-js-express/node-js-express-mongoose/app/root/root.controller.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ const express = require('express');
22
const RootService = require('./root.service');
33

44
class RootController {
5-
constructor() {
6-
this.router = express.Router();
5+
constructor() {
6+
this.router = express.Router();
77

8-
// Setting the routes for the root endpoint group
9-
this.router.get('/', this.info.bind(this)); // Binding is necessary for 'this' to refer to the class instance
10-
}
8+
// Setting the routes for the root endpoint group
9+
this.router.get('/', this.info.bind(this)); // Binding is necessary for 'this' to refer to the class instance
10+
}
1111

12-
getRouter() {
13-
return this.router;
14-
}
12+
getRouter() {
13+
return this.router;
14+
}
1515

16-
async info(request, response, next) {
17-
const message = RootService.info();
18-
response.status(200).send(message);
19-
}
16+
async info(request, response, next) {
17+
const message = RootService.info();
18+
response.status(200).send(message);
19+
}
2020
}
2121

2222
module.exports = RootController;
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class RootService {
2-
info() {
3-
return "Hello, World!";
4-
}
2+
info() {
3+
return 'Hello, World!';
4+
}
55
}
66

7-
module.exports = new RootService(); // Exporting an instance of the service
7+
module.exports = new RootService(); // Exporting an instance of the service

0 commit comments

Comments
 (0)