-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
71 lines (57 loc) · 2.04 KB
/
app.js
File metadata and controls
71 lines (57 loc) · 2.04 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// config env
require('dotenv').config()
// yarn init -> tạo json
// yarn express -> node_modules (chứa source code của các thư viện) + yarn.lock (lưu lịch sử)
const express = require('express') // config on app
const logger = require('morgan') // use for dev
const mongoClinet = require('mongoose') // connect mongodb
const bodyParser = require('body-parser')
// setup connect mongodb by mongoose
// connect success => then work else => catch work
mongoClinet.connect('mongodb://localhost/nodejsapistarter')
.then(() => console.log('Connected database from mongodb.'))
.catch((error) => console.error(`Connect database is failed with error which is ${error}`))
const app = express()
const userRoute = require('./routes/user')
const deckRoute = require('./routes/deck')
// Middlewares
// logger sẽ chạy trước đến request nên được gọi là middleware
// client - logger - handle
app.use(logger('dev'))
// sử dụng bodyParser
app.use(bodyParser.json())
// Routes
app.use('/users', userRoute)
app.use('/decks', deckRoute)
// Routes
app.get('/', (req, res, next) => {
return res.status(200).json({
message: "Server is OK"
})
})
// Catch 404 Errors and forward them to error handler
app.use((req, res, next) => {
// req = request
// res = respond
const err = new Error('Not Found')
err.status = 404
// if err -> next
next(err) // chuyển đến hàm hàm xử lý lỗi (Error handler function)
})
// Error handler function
app.use((err, req, res, next) => {
const error = app.get('env') == 'development' ? err: {}
const status = err.status || 500
// response to client
return res.status(status).json({
error: {
message: error.message
}
})
})
// Start the server
const port = app.get('port') || 3000
app.listen(port, () => console.log(`Server is listening on port ${port}`))
// What is development and production
// development: yarn add morgan --save-dev -> Cài sai: yarn remove morgan (check package.json to see more)
// yarn add morgan --dev