-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
30 lines (23 loc) · 1.01 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
const express = require ('express');
const path = require ('path');
const logger = require ('morgan');
const { dispatcher, middleware, errorMiddleware } = require ('./lib/fluture-express');
const errorHandler = require ('./middleware/error-handler');
const authenticate = require ('./middleware/authenticate');
module.exports.create = config => mongo => {
const dispatch = dispatcher (path.resolve (__dirname, 'actions')) ({ db: mongo.db('test-db'), config });
const handleErrors = errorMiddleware (errorHandler);
const auth = middleware (authenticate) ({ config });
const app = express ();
app.use (logger (config.env));
app.use (express.json ());
app.use (express.urlencoded ({ extended: false }));
/******** Routes ********/
app.post ('/login', dispatch ('login'));
app.post ('/register-user', dispatch ('register-user'));
app.get ('/items', auth, dispatch ('getallitems'));
app.post ('/item', auth, dispatch ('additem'));
/***** Routes END ******/
app.use (handleErrors);
return app;
}