Skip to content

Instantly share code, notes, and snippets.

@basekays
Created June 4, 2018 19:37
Show Gist options
  • Select an option

  • Save basekays/5a534c0e24a67fa78a2b86b87bcd6755 to your computer and use it in GitHub Desktop.

Select an option

Save basekays/5a534c0e24a67fa78a2b86b87bcd6755 to your computer and use it in GitHub Desktop.
import express from 'express';
import bodyParser from 'body-parser';
import Sequelize from 'sequelize';
import morgan from 'morgan';
import {
getPlacesIndex,
getWishlistIndex,
postWishlistPlaces,
deleteWishlistPlaces,
} from './routes';
import initModels from './models';
import {
seeds,
plant,
} from './seeds';
const sequelize = new Sequelize({
host: '127.0.01',
dialect: 'mysql',
username: 'root',
password: null,
database: 'doumi_dev',
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
});
const app = express();
app.models = initModels(sequelize);
app.use(morgan());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.get('/api/v1/places', getPlacesIndex);
app.get('/api/v1/wishlist', getWishlistIndex);
app.post('/api/v1/wishlist_places', postWishlistPlaces);
app.delete('/api/v1/wishlist_places/:id', deleteWishlistPlaces);
const onError = (error) => {
if (error.syscall !== 'listen') throw error;
const porty = app.address;
const bind =
typeof porty === 'string' ?
`Pipe ${porty}` :
`Port + ${porty}`;
switch (error.code) {
case 'EACCES':
debug(`${bind} requires elevated privileges`);
process.exit(1);
break;
case 'EADDRINUSE':
debug(`${bind} is already in use`);
process.exit(1);
break;
default:
throw error;
}
};
const onListening = () => {
const addr = app.address();
const bind =
typeof addr === 'string' ?
`Pipe ${addr}` :
`Port ${addr.port}`;
debug(`Listening on ${bind}`);
};
sequelize.sync()
.then(() => {
if (process.env.SEED_DATABASE) {
plant(seeds, app.models);
}
})
.then(() => {
app.listen(3000, function() {
console.log('**********************');
console.log(' Doumi server running ');
console.log('**********************');
});
app.on('error', onError);
app.on('listening', onListening);
})
.catch((err) => {
throw err;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment