107 lines
2.9 KiB
JavaScript
107 lines
2.9 KiB
JavaScript
var createError = require('http-errors');
|
|
var express = require('express');
|
|
var path = require('path');
|
|
var cookieParser = require('cookie-parser');
|
|
var logger = require('morgan');
|
|
const http = require('http');
|
|
const https = require('https');
|
|
const fs = require('fs');
|
|
|
|
const hostname = 'petrov.nexus';
|
|
const httpPort = 80;
|
|
const httpsPort = 443;
|
|
|
|
const httpsOptions = {
|
|
key: fs.readFileSync('./archive/server.key'),
|
|
cert: fs.readFileSync('./archive/petrov_nexus.crt'),
|
|
ca: fs.readFileSync('./archive/petrov_nexus.ca-bundle')
|
|
};
|
|
|
|
// 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 listRouter = require('./routes/list');
|
|
var photosRouter = require('./routes/api/photoRouter');
|
|
var movieRouter = require('./routes/api/movieRouter');
|
|
|
|
var app = express();
|
|
|
|
const httpServer = http.createServer(app);
|
|
const httpsServer = https.createServer(httpsOptions, app);
|
|
|
|
// 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('/list', listRouter);
|
|
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', {title:'Error'});
|
|
});
|
|
|
|
httpServer.listen(httpPort, () => {
|
|
console.log(`Server running at http://${hostname}:${httpPort}/`);
|
|
});
|
|
httpsServer.listen(httpsPort, () => {
|
|
console.log(`Server running at https://${hostname}:${httpsPort}/`);
|
|
});
|
|
|
|
|
|
|
|
|
|
module.exports = app;
|