1
+ const express = require ( "express" ) ;
2
+ const app = express ( ) ;
3
+ const morgan = require ( "morgan" ) ;
4
+ const bodyParser = require ( "body-parser" ) ;
5
+ const mongoose = require ( "mongoose" ) ;
6
+
7
+ const contractRoutes = require ( "./api/routes/contracts" ) ;
8
+ const walletRoutes = require ( "./api/routes/wallets" ) ;
9
+ const userRoutes = require ( "./api/routes/user" ) ;
10
+
11
+ mongoose . connect (
12
+ // require retryWrites=false
13
+ process . env . MONGO_ATLAS_ADDRESS
14
+ ) ;
15
+
16
+ mongoose . Promise = global . Promise ;
17
+
18
+ app . use ( morgan ( "dev" ) ) ;
19
+ app . use ( bodyParser . urlencoded ( { extended : false } ) ) ;
20
+ app . use ( bodyParser . json ( ) ) ;
21
+
22
+ app . use ( ( req , res , next ) => {
23
+ res . header ( "Access-Control-Allow-Origin" , "*" ) ;
24
+ res . header (
25
+ "Access-Control-Allow-Headers" ,
26
+ "Origin, X-Requested-With, Content-Type, Accept, Authorization"
27
+ ) ;
28
+ if ( req . method === "OPTIONS" ) {
29
+ res . header ( "Access-Control-Allow-Methods" , "PUT, POST, PATCH, DELETE, GET" ) ;
30
+ return res . status ( 200 ) . json ( { } ) ;
31
+ }
32
+ next ( ) ;
33
+ } ) ;
34
+
35
+ // Routes which should handle requests
36
+ app . use ( "/contracts" , contractRoutes ) ;
37
+ app . use ( "/wallets" , walletRoutes ) ;
38
+ app . use ( "/user" , userRoutes ) ;
39
+
40
+ app . use ( ( req , res , next ) => {
41
+ const error = new Error ( "Not found" ) ;
42
+ error . status = 404 ;
43
+ next ( error ) ;
44
+ } ) ;
45
+
46
+ app . use ( ( error , req , res , next ) => {
47
+ res . status ( error . status || 500 ) ;
48
+ res . json ( {
49
+ error : {
50
+ message : error . message
51
+ }
52
+ } ) ;
53
+ } ) ;
54
+
55
+ module . exports = app ;
0 commit comments