85 lines
2.2 KiB
JavaScript
85 lines
2.2 KiB
JavaScript
var createError = require('http-errors');
|
|
var express = require('express');
|
|
var path = require('path');
|
|
var cookieParser = require('cookie-parser');
|
|
var logger = require('morgan');
|
|
|
|
|
|
// vključimo mongoose in ga povežemo z MongoDB
|
|
var mongoose = require('mongoose');
|
|
var mongoDB = "mongodb://127.0.0.1/my_database";
|
|
mongoose.connect(mongoDB);
|
|
mongoose.set('strictQuery', false);
|
|
mongoose.Promise = global.Promise;
|
|
var db = mongoose.connection;
|
|
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
|
|
|
|
|
|
var oldRouter = require('./routes/main/1_0');
|
|
var newRouter = require('./routes/main/2_0');
|
|
var photosRouter = require('./routes/photoRouter');
|
|
var movieRouter = require('./routes/movieRouter');
|
|
|
|
var app = express();
|
|
|
|
// view engine setup
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
app.set('view engine', 'hbs');
|
|
|
|
app.use(logger('dev'));
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: false }));
|
|
app.use(cookieParser());
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
|
|
/**
|
|
* Vključimo session in connect-mongo.
|
|
* Connect-mongo skrbi, da se session hrani v bazi.
|
|
* Posledično ostanemo prijavljeni, tudi ko spremenimo kodo (restartamo strežnik)
|
|
*/
|
|
var session = require('express-session');
|
|
var MongoStore = require('connect-mongo');
|
|
app.use(session({
|
|
secret: 'work hard',
|
|
resave: true,
|
|
saveUninitialized: false,
|
|
store: MongoStore.create({mongoUrl: mongoDB})
|
|
}));
|
|
//Shranimo sejne spremenljivke v locals
|
|
//Tako lahko do njih dostopamo v vseh view-ih (glej layout.hbs)
|
|
app.use(function (req, res, next) {
|
|
res.locals.session = req.session;
|
|
next();
|
|
});
|
|
|
|
|
|
|
|
app.use('/', newRouter);
|
|
app.use('/old', oldRouter);
|
|
app.use('/api/photos', photosRouter);
|
|
app.use('/api/movies', movieRouter);
|
|
|
|
// 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');
|
|
});
|
|
|
|
const port = 80;
|
|
app.listen(port, () => {
|
|
console.log(`Example app listening on port ${port}`)
|
|
})
|
|
|
|
module.exports = app;
|