forked from m0nclous/api-on-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
52 lines (47 loc) · 1.74 KB
/
server.js
File metadata and controls
52 lines (47 loc) · 1.74 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const { MongoClient, ObjectID } = require('mongodb');
const dbConnect = require('./databases/db');
const ArtistValidator = require('./validators/artist');
const ArtistModel = require('./models/artist');
const ValidationError = require('./errors/validation-error');
const NotFoundError = require('./errors/not-found-error');
const ArtistController = require('./controllers/artist');
const ArtistRouter = require('./routes/artist');
const ErrorHandler = require('./middlewares/error-handler');
const {
DB_HOST = 'localhost',
DB_PORT = 27017,
DB_BASE = 'myApi',
DB_LOGIN = 'user',
DB_PASS = 'pwd',
ARTIST_MODEL_NAME = 'artists',
SERVER_HOST = 'localhost',
SERVER_PORT = 3000,
} = process.env;
const url = `mongodb://${DB_LOGIN}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_BASE}`;
const startMessage = `App started on http://${SERVER_HOST}:${SERVER_PORT}/`;
const start = async () => {
const db = await dbConnect(url, MongoClient);
const artistValidator = new ArtistValidator(ObjectID);
const artistModel = new ArtistModel(
db,
ARTIST_MODEL_NAME,
ObjectID,
artistValidator,
ValidationError,
NotFoundError,
);
const artistController = new ArtistController(artistModel);
const artistRouter = ArtistRouter(new express.Router(), artistController);
const errorHandler = new ErrorHandler(ValidationError, NotFoundError);
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/artists', artistRouter);
app.use(errorHandler.process);
// eslint-disable-next-line no-console
app.listen(SERVER_PORT, SERVER_HOST, () => console.info(startMessage));
};
start();