-
Notifications
You must be signed in to change notification settings - Fork 103
/
app.js
42 lines (34 loc) · 938 Bytes
/
app.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
const express = require('express');
const createError = require('http-errors');
const dotenv = require('dotenv').config();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Initialize DB
require('./initDB')();
const ProductRoute = require('./Routes/Product.route');
app.use('/products', ProductRoute);
//404 handler and pass to error handler
app.use((req, res, next) => {
/*
const err = new Error('Not found');
err.status = 404;
next(err);
*/
// You can use the above code if your not using the http-errors module
next(createError(404, 'Not found'));
});
//Error handler
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.send({
error: {
status: err.status || 500,
message: err.message
}
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log('Server started on port ' + PORT + '...');
});