Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
2.3 kB
3
Indexable
Never
const express = require('express');
const appServer = express();
const path = require('path')
const Exsession = require('express-session')
const mongodb_session = require('connect-mongodb-session')(Exsession);
const mongoose = require('mongoose');
const dbDet = "mongodb+srv://Biren:123@Biren@cluster0.nqzgp.mongodb.net/ProductMongoose?retryWrites=true&w=majority";
const userModel = require('./Model/authProduct')
const adminRoute = require('./Route/adminRoute')
const shopRoute = require('./Route/shopRoute')
const authRoute = require('./Route/authRoute')
const bodyParser = require('body-parser')
const multer = require('multer')

const storeValue = new mongodb_session({
    url: 'mongodb+srv://Biren:123@Biren@cluster0.nqzgp.mongodb.net/ProductMongoose?retryWrites=true&w=majority' ,
    collection: 'new-session'
})

appServer.use(bodyParser.urlencoded({extended:true}))


appServer.use(Exsession({secret:'secret_key',resave:false,saveUninitialized:false,store:storeValue}))

appServer.use(express.static(path.join(__dirname,'Public')))
appServer.use('/uploadImage',express.static(path.join(__dirname,'uploadImage')))

const fileStorage = multer.diskStorage({
    destination:(req,file,callback)=>{
        callback(null,'uploadImage')
    },
    filename:(req,file,callback)=>{
        callback(null, file.originalname)
    }
})

const fileFilter = (req,file,callback)=>{
    if(file.mimetype.includes("png") || file.mimetype.includes("jpg") || file.mimetype.includes("jpeg")){
        callback(null,true)
    }else{
        callback(null,false)
    }
}
appServer.use(multer({storage:fileStorage,fileFilter:fileFilter,limits:{fieldSize:1024*1024*5}}).single('pimage'));

appServer.set('view engine', 'ejs')
appServer.set('views', 'Views')

appServer.use((req,res,next) => {
    if(!req.session.user){
        return next()
    }
    userModel.findById(req.session.user._id).then(userValue => {
        req.user = userValue;
        console.log('app' + req.user);
        next()
    }).catch(err => console.log(err));
})

appServer.use(adminRoute)
appServer.use(shopRoute)
appServer.use(authRoute)


mongoose.connect(dbDet, {useNewUrlParser:true, useUnifiedTopology:true}).then(result => {
    appServer.listen(1111,()=>{
        console.log("server connect");
    })
}).catch(err => {
    console.log(err);
})