-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (42 loc) · 1.75 KB
/
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
44
45
46
47
//=============================================================================
// Basic Config
//=============================================================================
const express = require('express');
const cors = require('cors');
// instantiate express
const app = express();
app.set('port', process.env.PORT || 8000);
//=============================================================================
// Middleware
//=============================================================================
// `express.json` parses application/json request data and
// adds it to the request object as request.body
app.use(express.json());
// `express.urlencoded` parses x-ww-form-urlencoded request data and
// adds it to the request object as request.body
app.use(express.urlencoded({ extended: true }));
app.use(cors());
//=============================================================================
// ROUTES
//=============================================================================
// Redirect
app.get('/', (req, res) => {
res.redirect('/products');
});
/* START CONTROLLERS HERE */
const productsController = require('./controllers/productsController');
app.use('/products', productsController);
const usersController = require('./controllers/usersController');
app.use('/users', usersController);
/* END CONTROLLERS HERE */
app.use((err, req, res, next) => {
const statusCode = res.statusCode || 500;
const message = err.message || 'Internal Server Error';
res.status(statusCode).send(message);
});
//=============================================================================
// START SERVER
//=============================================================================
app.listen(app.get('port'), () => {
console.log(`✅ PORT: ${app.get('port')} 🌟`);
});