Nikola Petrov f240d4e7c5 format
2023-08-02 14:27:35 +02:00

87 lines
2.5 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 = 4080;
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 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')));
//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();
});
var mainRouter = require('./routes/main');
var listRouter = require('./routes/list');
var photosRouter = require('./routes/api/photoRouter');
var movieRouter = require('./routes/api/movieRouter');
var mailRouter = require('./routes/mail');
app.use('/', mainRouter);
app.use('/list', listRouter);
app.use('/api/photos', photosRouter);
app.use('/api/movies', movieRouter);
app.use('/mail', mailRouter);
// 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;