-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
44 lines (34 loc) · 1.04 KB
/
app.js
File metadata and controls
44 lines (34 loc) · 1.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
const express = require('express');
const logger = require('./config/appconfig').logger
// Import routes
const authRoutes = require('./routes/auth.routes')
const appartmentRoutes = require('./routes/appartments.routes');
// Defining constants
const app = express();
const port = process.env.port || 3000;
app.use(express.json());
// Generic end-point handler
app.all('*', (req, res, next) => {
console.log("Generic endpoint handler!");
next();
})
// Using routes
app.use('/api', authRoutes);
app.use('/api/appartments', appartmentRoutes);
// Handle all other (not found) endpoints.
app.all('*', (req, res, next) => {
console.log('Endpoint not found.')
const errorObject = {
message: 'Endpoint does not exist!',
code: 404,
date: new Date()
}
next(errorObject);
});
// Error handler
app.use((error, req, res, next) => {
logger.error('Error handler: ', error.message.toString());
res.status(error.code).json(error)
})
app.listen(port, ()=> console.log("listening on port "+port));
module.exports = app;