-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 97e4322
Showing
28 changed files
with
2,040 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Eduocean | ||
Educational website:) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var createError = require('http-errors'); | ||
var express = require('express'); | ||
var path = require('path'); | ||
var cookieParser = require('cookie-parser'); | ||
var logger = require('morgan'); | ||
var favicon = require('serve-favicon'); | ||
|
||
var compression = require('compression'); | ||
var helmet = require('helmet'); | ||
// Set up mongoose connection | ||
var mongoose = require("mongoose"); | ||
var dev_db_url = "mongodb+srv://eduocean:patryk08@cluster0.yatji.mongodb.net/eduocean?retryWrites=true&w=majority"; | ||
|
||
var mongoDB = process.env.MONGODB_URI || dev_db_url; | ||
mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true }); | ||
var db = mongoose.connection; | ||
db.on("error", console.error.bind(console, "MongoDB connection error:")); | ||
|
||
var indexRouter = require('./routes/index'); | ||
var usersRouter = require('./routes/users'); | ||
var catalogRouter = require('./routes/catalog.js'); //Import routes for "catalog" area of site | ||
|
||
|
||
var app = express(); | ||
|
||
// view engine setup | ||
app.set('views', path.join(__dirname, 'views')); | ||
app.set('view engine', 'pug'); | ||
|
||
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); | ||
app.use(logger('dev')); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
app.use(cookieParser()); | ||
app.locals.basedir = path.join(__dirname, 'public'); | ||
|
||
app.use(compression()); //Compress all routes | ||
app.use(helmet()); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
|
||
app.use('/', indexRouter); | ||
app.use('/users', usersRouter); | ||
app.use('/catalog', catalogRouter); // Add catalog routes to middleware chain. | ||
|
||
// 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'); | ||
}); | ||
|
||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var app = require('../app'); | ||
var debug = require('debug')('eduocean:server'); | ||
var http = require('http'); | ||
|
||
/** | ||
* Get port from environment and store in Express. | ||
*/ | ||
|
||
var port = normalizePort(process.env.PORT || '3000'); | ||
app.set('port', port); | ||
|
||
/** | ||
* Create HTTP server. | ||
*/ | ||
|
||
var server = http.createServer(app); | ||
|
||
/** | ||
* Listen on provided port, on all network interfaces. | ||
*/ | ||
|
||
server.listen(port); | ||
server.on('error', onError); | ||
server.on('listening', onListening); | ||
|
||
/** | ||
* Normalize a port into a number, string, or false. | ||
*/ | ||
|
||
function normalizePort(val) { | ||
var port = parseInt(val, 10); | ||
|
||
if (isNaN(port)) { | ||
// named pipe | ||
return val; | ||
} | ||
|
||
if (port >= 0) { | ||
// port number | ||
return port; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Event listener for HTTP server "error" event. | ||
*/ | ||
|
||
function onError(error) { | ||
if (error.syscall !== 'listen') { | ||
throw error; | ||
} | ||
|
||
var bind = typeof port === 'string' | ||
? 'Pipe ' + port | ||
: 'Port ' + port; | ||
|
||
// handle specific listen errors with friendly messages | ||
switch (error.code) { | ||
case 'EACCES': | ||
console.error(bind + ' requires elevated privileges'); | ||
process.exit(1); | ||
break; | ||
case 'EADDRINUSE': | ||
console.error(bind + ' is already in use'); | ||
process.exit(1); | ||
break; | ||
default: | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Event listener for HTTP server "listening" event. | ||
*/ | ||
|
||
function onListening() { | ||
var addr = server.address(); | ||
var bind = typeof addr === 'string' | ||
? 'pipe ' + addr | ||
: 'port ' + addr.port; | ||
debug('Listening on ' + bind); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
var Article = require('../models/article'); | ||
|
||
const { | ||
body, | ||
validationResult | ||
} = require('express-validator'); | ||
const MiniSearch = require("minisearch"); | ||
|
||
var isEmpty = function (obj) { | ||
return Object.keys(obj).length === 0; | ||
} | ||
|
||
exports.article_search = function (req, res, next) { | ||
body('query').trim().isEmpty().escape() // Validate input using express-validator | ||
|
||
Article.find({}).exec(function (err, list_articles) { //Search all the articles in db | ||
if (err) { | ||
return next(err); | ||
} | ||
let miniSearch = new MiniSearch({ | ||
fields: ["title", "desc", "genre", "lang"], | ||
storeFields: ['title', 'desc', 'genre', 'lang', 'link'], | ||
fuzzy: 0.2 | ||
}); | ||
miniSearch.addAll(list_articles) | ||
let results = miniSearch.search(req.query.query) //Search article matching query | ||
if (!isEmpty(results)) { // Got results | ||
res.render('article_list', { | ||
articles_list: results | ||
}) | ||
} else { | ||
var message = "Nie znaleziono artykułu" | ||
res.render('error', { | ||
message | ||
}) | ||
} | ||
}); | ||
} | ||
|
||
// Get random genre article | ||
const randomArticles = async () => { | ||
const categoryByTemplateKey = { | ||
randInf: 'Informatyka', | ||
randMath: 'Matematyka', | ||
randBiol: 'Biologia', | ||
randChem: 'Chemia', | ||
randRoz: 'Rozwój Osobisty', | ||
randHis: 'Historia', | ||
}; | ||
|
||
const articles = {}; | ||
for (let [key, categoryName] of Object.entries(categoryByTemplateKey)) { | ||
articles[key] = await Article.findOne({genre: categoryName}).exec(); | ||
} | ||
return articles; | ||
}; | ||
|
||
exports.randArt = function(req, res, next) { | ||
randomArticles().then(articles => { | ||
res.render('index', articles); | ||
}); | ||
}; | ||
|
||
// Display Article create form on GET. | ||
exports.article_create_get = function(req, res) { | ||
res.send('NIE ZAIMPLEMENTOWANE: Article create GET przepraszamy:('); | ||
}; | ||
|
||
// Handle Article create on POST. | ||
exports.article_create_post = function(req, res) { | ||
res.send('NIE ZAIMPLEMENTOWANE: Article create POST przepraszamy:('); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
var Genre = require('../models/genre'); | ||
|
||
// Display list of all Genre. | ||
exports.genre_list = function(req, res, next) { | ||
Genre.find() | ||
.exec(function (err, list_genres) { | ||
if (err) { return next(err); } | ||
//Successful, so render | ||
res.render('genre_list', { title: 'Author List', genre_list: list_genres }); | ||
}); | ||
}; | ||
|
||
// Display detail page for a specific Genre. | ||
exports.genre_detail = function(req, res) { | ||
res.send('NOT IMPLEMENTED: Genre detail: ' + req.params.id); | ||
}; | ||
|
||
// Display Genre create form on GET. | ||
exports.genre_create_get = function(req, res) { | ||
res.send('NOT IMPLEMENTED: Genre create GET'); | ||
}; | ||
|
||
// Handle Genre create on POST. | ||
exports.genre_create_post = function(req, res) { | ||
res.send('NOT IMPLEMENTED: Genre create POST'); | ||
}; | ||
|
||
// Display Genre delete form on GET. | ||
exports.genre_delete_get = function(req, res) { | ||
res.send('NOT IMPLEMENTED: Genre delete GET'); | ||
}; | ||
|
||
// Handle Genre delete on POST. | ||
exports.genre_delete_post = function(req, res) { | ||
res.send('NOT IMPLEMENTED: Genre delete POST'); | ||
}; | ||
|
||
// Display Genre update form on GET. | ||
exports.genre_update_get = function(req, res) { | ||
res.send('NOT IMPLEMENTED: Genre update GET'); | ||
}; | ||
|
||
// Handle Genre update on POST. | ||
exports.genre_update_post = function(req, res) { | ||
res.send('NOT IMPLEMENTED: Genre update POST'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var mongoose = require('mongoose'); | ||
|
||
var Schema = mongoose.Schema; | ||
|
||
var ArticleSchema = new Schema( | ||
{ | ||
title: {type: String, required: true}, | ||
desc: {type: String, required: true}, | ||
genre: {type: String, ref: 'Genre', required: true}, | ||
lang: {type: String, ref: 'Lang', required: true}, | ||
link: {type: String, required: true} | ||
} | ||
); | ||
|
||
//Export model | ||
module.exports = mongoose.model('Article', ArticleSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var mongoose = require('mongoose'); | ||
|
||
var Schema = mongoose.Schema; | ||
|
||
var Genre = new Schema( | ||
{ | ||
name: {type: String, required: true, min: 3, max: 100}, | ||
} | ||
); | ||
|
||
//Export model | ||
module.exports = mongoose.model('Genre', Genre); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var mongoose = require('mongoose'); | ||
|
||
var Schema = mongoose.Schema; | ||
|
||
var Lang = new Schema( | ||
{ | ||
name: {type: String, required: true, min: 3, max: 40}, | ||
} | ||
); | ||
|
||
//Export model | ||
module.exports = mongoose.model('Lang', Lang); |
Oops, something went wrong.