2020-01-14 13:25:13 +01:00
|
|
|
import express from 'express';
|
2020-04-22 17:24:15 +02:00
|
|
|
import bodyParser from 'body-parser';
|
2020-04-23 13:59:45 +02:00
|
|
|
import contentFilter from 'content-filter';
|
|
|
|
import mongoSanitize from 'mongo-sanitize';
|
2020-05-12 14:05:47 +02:00
|
|
|
import api from './api';
|
2020-04-22 17:24:15 +02:00
|
|
|
import db from './db';
|
2020-01-14 13:25:13 +01:00
|
|
|
|
2020-05-26 09:07:01 +02:00
|
|
|
// TODO: changelog
|
|
|
|
// TODO: check executing index.js/move everything needed into dist
|
|
|
|
// TODO: One condition per sample
|
|
|
|
// TODO: validation: VZ, Humidity: min/max value, DPT: filename
|
|
|
|
// TODO: condition values not needed on initial add
|
|
|
|
// TODO: add multiple samples at once
|
2020-01-14 13:25:13 +01:00
|
|
|
|
|
|
|
// tell if server is running in debug or production environment
|
2020-05-07 21:55:29 +02:00
|
|
|
console.info(process.env.NODE_ENV === 'production' ? '===== PRODUCTION =====' : process.env.NODE_ENV === 'test' ? '' :'===== DEVELOPMENT =====');
|
2020-01-14 13:25:13 +01:00
|
|
|
|
|
|
|
|
2020-04-22 17:24:15 +02:00
|
|
|
// mongodb connection
|
|
|
|
db.connect();
|
2020-04-20 16:17:43 +02:00
|
|
|
|
2020-01-14 13:25:13 +01:00
|
|
|
// create Express app
|
|
|
|
const app = express();
|
|
|
|
app.disable('x-powered-by');
|
|
|
|
|
|
|
|
// get port from environment, defaults to 3000
|
|
|
|
const port = process.env.PORT || 3000;
|
|
|
|
|
2020-04-22 17:24:15 +02:00
|
|
|
//middleware
|
|
|
|
app.use(express.json({ limit: '5mb'}));
|
|
|
|
app.use(express.urlencoded({ extended: false, limit: '5mb' }));
|
|
|
|
app.use(bodyParser.json());
|
2020-04-23 13:59:45 +02:00
|
|
|
app.use(contentFilter()); // filter URL query attacks
|
|
|
|
app.use((req, res, next) => { // filter body query attacks
|
|
|
|
req.body = mongoSanitize(req.body);
|
|
|
|
next();
|
|
|
|
});
|
2020-04-22 17:24:15 +02:00
|
|
|
app.use((err, req, res, ignore) => { // bodyParser error handling
|
|
|
|
res.status(400).send({status: 'Invalid JSON body'});
|
|
|
|
});
|
2020-04-23 13:59:45 +02:00
|
|
|
app.use((req, res, next) => { // no database connection error
|
|
|
|
if (db.getState().db) {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.status(500).send({status: 'Internal server error'});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
app.use(require('./helpers/authorize')); // handle authentication
|
2020-04-22 17:24:15 +02:00
|
|
|
|
2020-01-14 13:25:13 +01:00
|
|
|
// require routes
|
2020-05-26 09:07:01 +02:00
|
|
|
app.use('/api', require('./routes/root'));
|
|
|
|
app.use('/api', require('./routes/sample'));
|
|
|
|
app.use('/api', require('./routes/material'));
|
|
|
|
app.use('/api', require('./routes/template'));
|
|
|
|
app.use('/api', require('./routes/user'));
|
|
|
|
app.use('/api', require('./routes/condition'));
|
|
|
|
app.use('/api', require('./routes/measurement'));
|
2020-01-14 13:25:13 +01:00
|
|
|
|
2020-04-29 15:07:07 +02:00
|
|
|
// static files
|
|
|
|
app.use('/static', express.static('static'));
|
|
|
|
|
2020-01-14 13:25:13 +01:00
|
|
|
// Swagger UI
|
2020-05-26 09:07:01 +02:00
|
|
|
app.use('/api-doc', api.serve(), api.setup());
|
2020-01-14 13:25:13 +01:00
|
|
|
|
2020-04-22 17:24:15 +02:00
|
|
|
app.use((req, res) => { // 404 error handling
|
|
|
|
res.status(404).json({status: 'Not found'});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use((err, req, res, ignore) => { // internal server error handling
|
|
|
|
console.error(err);
|
|
|
|
res.status(500).json({status: 'Internal server error'});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2020-01-14 13:25:13 +01:00
|
|
|
// hook up server to port
|
2020-04-22 17:24:15 +02:00
|
|
|
const server = app.listen(port, () => {
|
2020-05-07 21:55:29 +02:00
|
|
|
console.info(process.env.NODE_ENV === 'test' ? '' : `Listening on http://localhost:${port}`);
|
2020-01-14 13:25:13 +01:00
|
|
|
});
|
2020-04-22 17:24:15 +02:00
|
|
|
|
|
|
|
module.exports = server;
|