This commit is contained in:
Nikola Petrov 2023-08-02 14:27:35 +02:00
parent 96099f751b
commit f240d4e7c5
12 changed files with 164 additions and 191 deletions

30
app.js
View File

@ -27,13 +27,6 @@ var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:')); 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 mailRouter = require('./routes/mail');
var app = express(); var app = express();
const httpServer = http.createServer(app); const httpServer = http.createServer(app);
@ -49,20 +42,6 @@ app.use(express.urlencoded({ extended: false }));
app.use(cookieParser()); app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public'))); 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 //Shranimo sejne spremenljivke v locals
//Tako lahko do njih dostopamo v vseh view-ih (glej layout.hbs) //Tako lahko do njih dostopamo v vseh view-ih (glej layout.hbs)
app.use(function (req, res, next) { app.use(function (req, res, next) {
@ -70,10 +49,13 @@ app.use(function (req, res, next) {
next(); 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('/', newRouter);
app.use('/old', oldRouter);
app.use('/list', listRouter); app.use('/list', listRouter);
app.use('/api/photos', photosRouter); app.use('/api/photos', photosRouter);
app.use('/api/movies', movieRouter); app.use('/api/movies', movieRouter);

View File

@ -12,7 +12,7 @@ module.exports = {
* movieController.list() * movieController.list()
*/ */
list: function (req, res) { list: function (req, res) {
MovieModel.find({}, {_id: 0, __v: 0}) MovieModel.find({}, { _id: 0, __v: 0 })
.then(movies => { .then(movies => {
return res.json(movies); return res.json(movies);
}) })
@ -46,8 +46,8 @@ module.exports = {
const mJson = await fetch(uri); const mJson = await fetch(uri);
const mData = await mJson.json(); const mData = await mJson.json();
if(mData.Response == 'False'){ if (mData.Response == 'False') {
return res.status(404).json({message: 'wrong code'}); return res.status(404).json({ message: 'wrong code' });
} }
const movie = new MovieModel({ const movie = new MovieModel({
@ -55,7 +55,6 @@ module.exports = {
title: mData.Title, title: mData.Title,
released: mData.Released, released: mData.Released,
webImg: mData.Poster, webImg: mData.Poster,
genre: mData.Genre
}); });
const savedMovie = await movie.save(); const savedMovie = await movie.save();
@ -72,9 +71,8 @@ module.exports = {
var id = req.body.code; var id = req.body.code;
const passp = req.body.pass; const passp = req.body.pass;
try try {
{ const userFound = await UserModel.findOne({ pass: passp });
const userFound = await UserModel.findOne({pass: passp});
if (!userFound) { if (!userFound) {
return res.status(404).json({ message: 'Wrong password' }); return res.status(404).json({ message: 'Wrong password' });
} }
@ -86,8 +84,7 @@ module.exports = {
return res.status(204).json(); return res.status(204).json();
} }
catch(err) catch (err) {
{
return res.status(500).json({ message: 'Error when deleting the movie.' }); return res.status(500).json({ message: 'Error when deleting the movie.' });
} }
}, },

View File

@ -32,7 +32,7 @@ module.exports = {
show: function (req, res) { show: function (req, res) {
var id = req.params.id; var id = req.params.id;
PhotoModel.findOne({_id: id}, function (err, photo) { PhotoModel.findOne({ _id: id }, function (err, photo) {
if (err) { if (err) {
return res.status(500).json({ return res.status(500).json({
message: 'Error when getting photo.', message: 'Error when getting photo.',
@ -119,7 +119,7 @@ module.exports = {
}); });
}, },
publish: function(req, res){ publish: function (req, res) {
return res.render('photo/publish'); return res.render('photo/publish');
} }
}; };

View File

@ -2,11 +2,10 @@ var mongoose = require('mongoose');
var Schema = mongoose.Schema; var Schema = mongoose.Schema;
var movieSchema = new Schema({ var movieSchema = new Schema({
'title' : String, 'title': String,
'released' : String, 'released': String,
'webImg' : String, 'webImg': String,
'genre' : String, 'code': String
'code' : String
}); });
module.exports = mongoose.model('movie', movieSchema); module.exports = mongoose.model('movie', movieSchema);

View File

@ -2,8 +2,8 @@ var mongoose = require('mongoose');
var Schema = mongoose.Schema; var Schema = mongoose.Schema;
var photoSchema = new Schema({ var photoSchema = new Schema({
'name' : String, 'name': String,
'path' : String 'path': String
}); });
module.exports = mongoose.model('photo', photoSchema); module.exports = mongoose.model('photo', photoSchema);

View File

@ -2,8 +2,8 @@ var mongoose = require('mongoose');
var Schema = mongoose.Schema; var Schema = mongoose.Schema;
var userSchema = new Schema({ var userSchema = new Schema({
'pass' : String, 'pass': String,
'key' : String, 'key': String,
}); });
module.exports = mongoose.model('user', userSchema); module.exports = mongoose.model('user', userSchema);

View File

@ -1,7 +1,7 @@
var express = require('express'); var express = require('express');
// Vključimo multer za file upload // Vključimo multer za file upload
var multer = require('multer'); var multer = require('multer');
var upload = multer({dest: 'public/UpImage/'}); var upload = multer({ dest: 'public/UpImage/' });
var router = express.Router(); var router = express.Router();
var photoController = require('../../controllers/photoController.js'); var photoController = require('../../controllers/photoController.js');

View File

@ -3,7 +3,7 @@ const fs = require('fs');
var router = express.Router(); var router = express.Router();
/* GET home page. */ /* GET home page. */
router.get('/', function(req, res, next) { router.get('/', function (req, res, next) {
data = { data = {
title: 'Movies', title: 'Movies',

View File

@ -3,7 +3,7 @@ var express = require('express');
var router = express.Router(); var router = express.Router();
/* GET home page. */ /* GET home page. */
router.get('/', function(req, res, next) { router.get('/', function (req, res, next) {
res.redirect('https://privateemail.com/'); res.redirect('https://privateemail.com/');
}); });

13
routes/main.js Normal file
View File

@ -0,0 +1,13 @@
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function (req, res, next) {
res.render('main/2_0', { title: 'Nikola Petrov', disableBootStrap: true });
});
router.get('/old', function (req, res, next) {
res.render('main/1_0', { title: 'Nikola Petrov' });
});
module.exports = router;

View File

@ -1,9 +0,0 @@
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('main/1_0', {title:'Nikola Petrov'});
});
module.exports = router;

View File

@ -1,9 +0,0 @@
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('main/2_0', {title:'Nikola Petrov', disableBootStrap: true});
});
module.exports = router;