-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
115 lines (103 loc) · 3.53 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import {Master} from "./models";
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const sassMiddleware = require('node-sass-middleware');
const {ApolloServer, ApolloError} = require('apollo-server-express');
import { v4 } from "uuid";
const typeDefs = require('./schema/typeDefs.js')
const resolvers = require('./schema/resolver.js')
const mongoose = require('mongoose');
import * as AppModels from './models';
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const {MNEMONIC} =require('./config');
const {checkMaster}= require('./helpers/Walletfunctions.js');
import AuthMiddleware from './middleware/auth.js';
import {join} from "path";
import './helpers/Web3Wrapper'
let cors=require('cors');
var app = express();
const uri = "mongodb+srv://qasim:[email protected]/dappslablocally?retryWrites=true&w=majority";
mongoose.connect(uri);
mongoose.Promise = global.Promise;
mongoose.connection.once('open', () => {
console.log(' 🍃 connected to mongoDB mLab');
app.listen(4000, () => {
console.log('🚀 now listening for requests on port 4000');
});
(async()=>{await checkMaster()})()
})
app.use(cors());
// view engine setup
app.use(express.static(join(__dirname, './uploads')));
app.use(express.static(join(__dirname, './dapps')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(sassMiddleware({
src: path.join(__dirname, 'public'),
dest: path.join(__dirname, 'public'),
indentedSyntax: true, // true = .sass and false = .scss
sourceMap: true
}));
app.use(express.static(path.join(__dirname, 'public')));
app.use(AuthMiddleware)
app.use('/', indexRouter);
app.use('/users', usersRouter);
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: true,
playground: true,
context: ({req}) => {
let {
user,
isAuth,
} = req;
return {
req,
user,
isAuth,
...AppModels,
};
},
formatError:(err)=> {
if (err.message.startsWith("Authentication Must Be Provided")) {
return new Error('Authentication Must Be Provided');
}else if (err.message.startsWith("Database Error")) {
return new Error('Database Error');
}else if (err.extensions.exception.name === 'ValidationError') {
console.log("validation Error",err.extensions.exception.errors)
return new Error(err.extensions.exception.errors);
}else if (err.originalError instanceof ApolloError) {
return err;
} else{
const errId = v4();
console.log("errId: ", errId);
console.log(err);
return new Error('Internal Server Error: '+errId);
// return err
}
},
});
server.applyMiddleware({app});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;