Skip to content
Snippets Groups Projects
app.js 2.03 KiB
Newer Older
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
Alexandre Roulois's avatar
Alexandre Roulois committed
const i18next = require('i18next');
const Backend = require('i18next-fs-backend');
const i18nextMiddleware = require('i18next-http-middleware');

// internationalization
i18next
	.use(Backend)
	.use(i18nextMiddleware.LanguageDetector)
	.init({
		backend: {
			loadPath: __dirname + '/locales/{{lng}}.json'
		},
		fallbackLng: 'en',
		preload: ['en', 'fr'],
		detection: {
			order: ['querystring', 'cookie'],
Alexandre Roulois's avatar
Alexandre Roulois committed
			lookupCookie: 'lang',
			lookupQuerystring: 'lang',
			caches: ['cookie']
		}
});

var indexRouter = require('./routes/index');

var app = express();

Alexandre Roulois's avatar
Alexandre Roulois committed
// serve additional libraries
app.use("/stylesheets", express.static(path.join(__dirname, "node_modules/bootstrap/dist/css")))
app.use("/javascripts", express.static(path.join(__dirname, "node_modules/bootstrap/dist/js")))
ROULOIS Alexandre's avatar
ROULOIS Alexandre committed
app.use("/stylesheets", express.static(path.join(__dirname, "node_modules/bootstrap-icons/font")) )
Alexandre Roulois's avatar
Alexandre Roulois committed
app.use(["/javascripts", "/stylesheets"], express.static(path.join(__dirname, "node_modules/leaflet/dist")) )
app.use("/javascripts", express.static(path.join(__dirname, "node_modules/jquery/dist")))
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

Alexandre Roulois's avatar
Alexandre Roulois committed
app.use(i18nextMiddleware.handle(i18next));

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')));

app.use('/', indexRouter);

// catch 404 and forward to error handler
Alexandre Roulois's avatar
Alexandre Roulois committed
app.use((req, res, next) => {
	res.status(404);
	res.render('404', { title: '404' });
});

// error handler
app.use(function(err, req, res, next) {
Alexandre Roulois's avatar
Alexandre Roulois committed
	// set locals, only providing error in development
	res.locals.message = err.message;
	res.locals.error = req.app.get('env') === 'development' ? err : {};
Alexandre Roulois's avatar
Alexandre Roulois committed
	// render the error page
	res.status(err.status || 500);
	res.render('error');
});

module.exports = app;