forked from fmr683/aux-shopping-cart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
69 lines (51 loc) · 1.85 KB
/
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
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
var express = require('express');
var logger = require('morgan');
var cors = require('cors');
const expressSanitizer = require('express-sanitizer');
var session = require('express-session');
var config = require('config');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
// Libraries
var responseMessages = require('./lib/response-messages');
var authentication = require('./middlewares/authentication');
// Routes
var index = require('./routes/index');
var usersRouter = require('./routes/users');
var productsRouter = require('./routes/products');
var cartsRouter = require('./routes/carts');
var app = express();
app.use(session({ secret: config.get('express-session-secret'), saveUninitialized: true, resave: true, cookie: { maxAge: 3600000 }})); // 1 Hours
// Enable CORS
app.use(cors());
// view engine setup
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// Mount express-sanitizer middleware here
app.use(expressSanitizer());
// Authenticate all requests
app.use(function(req, res, next) {
authentication.authRequest(req, res, next);
});
// API Calls
app.use('/', index);
app.use('/v1/users', usersRouter);
app.use('/v1/products', productsRouter);
app.use('/v1/cart', cartsRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
response = responseMessages.commonResponse(responseMessages.NOT_FOUND);
res.status(404).json(response);
});
// error handler
app.use(function(err, req, res, next) {
response = responseMessages.commonResponse(responseMessages.UNKNOWN_ERROR, "", "", err.message);
res.status(500).json(response);
});
module.exports = app;