85 lines
2.5 KiB
JavaScript

const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
let exampleApp = true;
if (!exampleApp) {
// vključimo mongoose in ga povežemo z MongoDB
const mongoose = require('mongoose');
const mongoDB = "mongodb://127.0.0.1/projekt";
mongoose.connect(mongoDB);
mongoose.set('strictQuery', false);
mongoose.Promise = global.Promise;
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
}
// routes
const usersRouter = require('./routes/userRoutes');
const locationRouter = require('./routes/locationRoutes');
const rawCameraData = require('./routes/rawCameraDataRoutes');
const hourlyData = require('./routes/hourlyDataRoutes');
const dailyData = require('./routes/dailyDataRoutes');
const dataRouter = require('./routes/dataRouter');
const counterRouter = require('./routes/counterRouter');
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// const logger = require('morgan');
// 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')));
if (!exampleApp) {
const session = require('express-session');
const MongoStore = require('connect-mongo');
app.use(session({
secret: 'work hard',
resave: true,
saveUninitialized: false,
//store: MongoStore.create({ mongoUrl: mongoDB })
}));
}
// catch 404 and forward to error handler
app.use(function (req, res, next) {
res.locals.session = req.session;
next();
});
if (!exampleApp) {
app.use('/api/users', usersRouter);
app.use('/api/rawCameraData', rawCameraData);
app.use('/api/hourlyData', hourlyData);
app.use('/api/dailyData', dailyData);
app.use('/api/counter', counterRouter);
}
app.use('/api/location', locationRouter);
app.use('/api/data', dataRouter);
// 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');
//console.log(req.socket.remoteAddress);
});
module.exports = app;