-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
124 lines (101 loc) · 3.9 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
116
117
118
119
120
121
122
123
124
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var sassMiddleware = require('node-sass-middleware');
var bodyParser = require('body-parser');
var session = require('express-session');
var methodOverride = require('method-override');
var flash = require('connect-flash');
var mongoose = require('mongoose');
var passport = require('passport');
var Web3 = require('web3');
var favicon = require('serve-favicon');
var index = require('./routes/index');
var users = require('./routes/users');
var seller = require('./routes/seller');
var products = require('./routes/products');
var manager = require('./routes/manager');
var notice = require('./routes/notice');
var passportConfig = require('./lib/passport-config'); //passport 로그인
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
if (app.get('env') === 'development') {
app.locals.pretty = true;
}
app.locals.moment = require('moment'); //시간 라이브러리
app.locals.querystring = require('querystring');
//mongodb 연결
mongoose.Promise = global.Promise; // ES6 Native Promise를 mongoose에서 사용한다.
const connStr = 'mongodb+srv://seonghunYang:hj@[email protected]/test?retryWrites=true&w=majority' ;
mongoose.connect(connStr, {useNewUrlParser: true });
mongoose.connection.on('error', console.error);
// Favicon은 웹사이트의 대표 아이콘입니다. Favicon을 만들어서 /public에 둡시다.
// https://www.favicon-generator.org/ 여기서 만들어볼 수 있어요.
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(methodOverride('_method', {methods: ['POST', 'GET']}));
app.use(sassMiddleware({
src: path.join(__dirname, 'public'),
dest: path.join(__dirname, 'public'),
indentedSyntax: true, // true = .sass and false = .scss
sourceMap: true
}));
const sessionStore = new session.MemoryStore();
const sessionId = 'mjoverflow.sid';
const sessionSecret = 'mymymymymoney'
// session을 사용할 수 있도록.
app.use(session({
name: sessionId,
resave: true,
saveUninitialized: true,
store: sessionStore,
secret: sessionSecret
}));
app.use(flash()); // flash message를 사용할 수 있도록
app.use(express.static(path.join(__dirname, 'public')));
//=======================================================
// Passport 초기화
//=======================================================
app.use(passport.initialize());
app.use(passport.session());
passportConfig(passport);
// pug의 local에 현재 사용자 정보와 flash 메시지를 전달하자.
app.use(function(req, res, next) {
res.locals.currentUser = req.user; // passport는 req.user로 user정보 전달
res.locals.flashMessages = req.flash();
next();
});
//web3 연결
const provider = new Web3.providers.HttpProvider(" https://ropsten.infura.io/v3/1aaf0adf6b8c495190259f11e9f997ea");
const web3 = new Web3(provider);
web3.eth.net.isListening()
.then(() => console.log('web3 is connected'))
.catch(e => console.log('wow. something went wrong'));
app.use('/', index);
app.use('/users', users);
app.use('/seller', seller);
require('./routes/auth')(app, passport);
app.use('/products', products);
app.use('/manager', manager);
app.use('/notice', notice);
// 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;