|
|
|
// require( 'trace-unhandled/register' );
|
|
|
|
|
|
|
|
import path from 'path'
|
|
|
|
import dotenv from 'dotenv'
|
|
|
|
|
|
|
|
dotenv.config({ path: path.join(__dirname, '../.env') });
|
|
|
|
|
|
|
|
import http from 'http';
|
|
|
|
import express, { Request, Response } from 'express';
|
|
|
|
import bodyParser from 'body-parser'
|
|
|
|
import cookieParser from 'cookie-parser'
|
|
|
|
import session from 'express-session'
|
|
|
|
import redis from 'redis'
|
|
|
|
import connectRedis from 'connect-redis'
|
|
|
|
import passport from 'passport'
|
|
|
|
import { authRouter, initPassport } from './auth/passport';
|
|
|
|
import register from '@react-ssr/express/register';
|
|
|
|
import mongoose, { Document, Schema } from 'mongoose'
|
|
|
|
|
|
|
|
const RedisStore = connectRedis(session);
|
|
|
|
const redisClient = redis.createClient({
|
|
|
|
host: '127.0.0.1',
|
|
|
|
port: 6379,
|
|
|
|
password: process.env.REDIS_PASSWORD
|
|
|
|
})
|
|
|
|
|
|
|
|
redisClient.on('error', (error) => {
|
|
|
|
console.error(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
redisClient.on('connect', () => {
|
|
|
|
console.log('Connection to Redis has been established successfully.');
|
|
|
|
});
|
|
|
|
|
|
|
|
mongoose.connect('mongodb://localhost:27017', {
|
|
|
|
useCreateIndex: true,
|
|
|
|
useNewUrlParser: true,
|
|
|
|
useUnifiedTopology: true,
|
|
|
|
user: process.env.MONGODB_USER,
|
|
|
|
pass: process.env.MONGODB_PASSWORD,
|
|
|
|
dbName: 'museum'
|
|
|
|
}, () => {
|
|
|
|
console.log('Connection to MongoDB has been established successfully.')
|
|
|
|
})
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
app.set('trust proxy', 1);
|
|
|
|
const server = http.createServer(app);
|
|
|
|
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
app.use(bodyParser.urlencoded({ extended: false }));
|
|
|
|
app.use(cookieParser(process.env.SESSION_SECRET));
|
|
|
|
app.use(
|
|
|
|
session({
|
|
|
|
store: new RedisStore({ client: redisClient }),
|
|
|
|
secret: process.env.SESSION_SECRET as string,
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.session());
|
|
|
|
initPassport();
|
|
|
|
app.use('/auth', authRouter);
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
await register(app);
|
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
console.log(req.session)
|
|
|
|
const user = { name: 'World' };
|
|
|
|
res.render('index', { user });
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(3000, () => {
|
|
|
|
console.log('> Ready on http://localhost:3000');
|
|
|
|
});
|
|
|
|
})();
|