-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
43 lines (35 loc) · 876 Bytes
/
index.js
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
const express = require('express');
const app = express();
const port = 3000;
// router
const router = require('./mongoose/router');
app.use(router);
// menangani request body
app.use(express.urlencoded({ extended: true }))
// parse JSON
app.use(express.json())
// middleware log
const log = (req, res, next) => {
console.log(Date.now() + ' ' + req.ip + ' ' + req.originalUrl)
next()
}
app.use(log)
// middleware menangani 404
const notFound = (req, res, next) => {
res.json({
status: 'error',
message: 'resource tidak ditemukan',
})
}
app.use(notFound)
// error handling
const errorHandling = (err, req, res, next) => {
res.json({
status: 'error',
message: 'terjadi kesalahan pada server',
})
}
app.use(errorHandling)
app.listen(port, () => {
console.log(`Server berjalan di http:://localhost/${port}`);
})