edit server

This commit is contained in:
Francesco Esposito 2019-08-07 11:46:42 +02:00
parent 4f85873d1f
commit 591601a132
1 changed files with 43 additions and 15 deletions

View File

@ -1,7 +1,9 @@
import express from 'express';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
import cors from 'cors';
import logger from './logger';
import users from './routes/users';
import privateUsers from './routes/PrivateUsers';
import privateTickets from './routes/PrivateTickets';
@ -9,28 +11,37 @@ import categories from './routes/categories';
import tickets from './routes/tickets';
import authentication from './helpers/authentication';
const mongoose = require('./config/database');
require('dotenv').config();
const {
DB_URL,
DB_USER,
DB_PASSWORD,
SECRET_KEY,
PORT,
} = process.env;
const options = {
useNewUrlParser: true,
user: DB_USER,
pass: DB_PASSWORD,
};
mongoose.connect(DB_URL, options);
mongoose.Promise = global.Promise;
const app = express();
<<<<<<< HEAD
app.set('secretKey', 'ciccio'); // sostituire con variabili ambiente (env)
app.set('secretKey', SECRET_KEY);
app.disable('etag');
=======
app.set('secretKey', 'ciccio'); // sostituire con variabili ambiente
>>>>>>> 32333cc9f65fbfb1b2804906d69a379ea71010c0
// connection to mongodb
mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection error:'));
mongoose.connection.on('error', err => logger.error(`error: ${err}`));
// middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
<<<<<<< HEAD
=======
app.disable('etag');
>>>>>>> 32333cc9f65fbfb1b2804906d69a379ea71010c0
app.use(cors());
// routes
@ -42,7 +53,24 @@ app.use('/api/categories', categories);
app.use('/api/tickets', authentication, privateTickets);
app.use('/api/users', authentication, privateUsers);
// listen
app.listen(3001, () => {
console.log('Server started');
app.use((err, req, res, next) => {
logger.error(err.stack);
next();
});
// listen
app.listen(PORT, () => {
logger.info('Server started');
});
process
.on('unhandledRejection', reason => logger.error(reason))
.on('uncaughtException', (err) => {
logger.error(err);
process.exit(1);
})
.on('SIGINT', () => {
logger.info('Server stopped');
process.exit(0);
});