Skip to content

Commit dd40ecf

Browse files
committed
Refactor.
Simplification of the structure.
1 parent f93f2ac commit dd40ecf

File tree

5 files changed

+8
-57
lines changed

5 files changed

+8
-57
lines changed

app.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ const express = require('express')
22
const logger = require('morgan')
33
const cors = require('cors')
44

5-
const indexRouter = require('./routes/index')
5+
const contactsRouter = require('./routes/api/contacts')
66

77
const app = express()
88

9-
const formatsLogger = app.get('env') === 'development' ? 'dev' : 'combined'
9+
const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short'
1010

1111
app.use(logger(formatsLogger))
1212
app.use(cors())
1313
app.use(express.json())
1414

15-
app.use('/api/contacts', indexRouter)
15+
app.use('/api/contacts', contactsRouter)
1616

1717
app.use((req, res) => {
1818
res.status(404).json({ message: 'Not found' })

bin/server.js

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,7 @@
1-
#!/usr/bin/env node
2-
31
const app = require('../app')
4-
const debug = require('debug')('template:server')
5-
const http = require('http')
6-
7-
const normalizePort = (val) => {
8-
const port = parseInt(val, 10)
9-
10-
if (isNaN(port)) {
11-
return val
12-
}
13-
14-
if (port >= 0) {
15-
return port
16-
}
17-
18-
return false
19-
}
20-
21-
const port = normalizePort(process.env.PORT || '3000')
22-
app.set('port', port)
23-
24-
const server = http.createServer(app)
25-
26-
server.listen(port)
27-
server.on('error', onError)
28-
server.on('listening', onListening)
29-
30-
function onError(error) {
31-
if (error.syscall !== 'listen') {
32-
throw error
33-
}
34-
35-
const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port
362

37-
switch (error.code) {
38-
case 'EACCES':
39-
console.error(bind + ' requires elevated privileges')
40-
process.exit(1)
41-
case 'EADDRINUSE':
42-
console.error(bind + ' is already in use')
43-
process.exit(1)
44-
default:
45-
throw error
46-
}
47-
}
3+
const PORT = process.env.PORT || 3000
484

49-
function onListening() {
50-
const addr = server.address()
51-
const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port
52-
debug('Listening on ' + bind)
53-
}
5+
app.listen(PORT, () => {
6+
console.log(`Server running. Use our API on port: ${PORT}`)
7+
})

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
"private": true,
55
"scripts": {
66
"start": "cross-env NODE_ENV=production node ./bin/server.js",
7-
"start:dev": "nodemon ./bin/server.js",
8-
"start:debug": "cross-env DEBUG=* nodemon ./bin/server.js",
7+
"start:dev": "cross-env NODE_ENV=development nodemon ./bin/server.js",
98
"lint": "eslint **/*.js",
109
"lint:fix": "eslint --fix **/*.js"
1110
},
1211
"dependencies": {
1312
"cors": "2.8.5",
1413
"cross-env": "7.0.3",
15-
"debug": "4.3.1",
1614
"express": "4.17.1",
1715
"morgan": "1.10.0"
1816
},

readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,5 @@
2727

2828
- `npm start` — старт сервера в режиме production
2929
- `npm run start:dev` — старт сервера в режиме разработки (development)
30-
- `npm run start:debug` — старт сервера в режиме DEBUG
3130
- `npm run lint` — запустить выполнение проверки кода с eslint, необходимо выполнять перед каждым PR и исправлять все ошибки линтера
3231
- `npm lint:fix` — та же проверка линтера, но с автоматическими исправлениями простых ошибок
File renamed without changes.

0 commit comments

Comments
 (0)