1
0
mirror of https://github.com/franjsco/tick3t-api synced 2024-12-22 04:56:36 +01:00

edit server

This commit is contained in:
Francesco Esposito 2019-08-07 11:46:42 +02:00
parent 4f85873d1f
commit 591601a132

View File

@ -1,7 +1,9 @@
import express from 'express'; import express from 'express';
import mongoose from 'mongoose';
import bodyParser from 'body-parser'; import bodyParser from 'body-parser';
import cors from 'cors'; import cors from 'cors';
import logger from './logger';
import users from './routes/users'; import users from './routes/users';
import privateUsers from './routes/PrivateUsers'; import privateUsers from './routes/PrivateUsers';
import privateTickets from './routes/PrivateTickets'; import privateTickets from './routes/PrivateTickets';
@ -9,28 +11,37 @@ import categories from './routes/categories';
import tickets from './routes/tickets'; import tickets from './routes/tickets';
import authentication from './helpers/authentication'; 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(); const app = express();
<<<<<<< HEAD app.set('secretKey', SECRET_KEY);
app.set('secretKey', 'ciccio'); // sostituire con variabili ambiente (env)
app.disable('etag'); app.disable('etag');
=======
app.set('secretKey', 'ciccio'); // sostituire con variabili ambiente
>>>>>>> 32333cc9f65fbfb1b2804906d69a379ea71010c0
// connection to mongodb // connection to mongodb
mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection error:')); mongoose.connection.on('error', err => logger.error(`error: ${err}`));
// middleware // middleware
app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); app.use(bodyParser.json());
<<<<<<< HEAD
=======
app.disable('etag');
>>>>>>> 32333cc9f65fbfb1b2804906d69a379ea71010c0
app.use(cors()); app.use(cors());
// routes // routes
@ -42,7 +53,24 @@ app.use('/api/categories', categories);
app.use('/api/tickets', authentication, privateTickets); app.use('/api/tickets', authentication, privateTickets);
app.use('/api/users', authentication, privateUsers); app.use('/api/users', authentication, privateUsers);
// listen app.use((err, req, res, next) => {
app.listen(3001, () => { logger.error(err.stack);
console.log('Server started'); 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);
});