This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
FunctionProject/api/app.js

89 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-03-16 22:55:32 +01:00
/* Modules */
2020-08-03 12:04:07 +02:00
require('dotenv').config()
const path = require('path')
const express = require('express')
const helmet = require('helmet')
const cors = require('cors')
const morgan = require('morgan')
const redirectToHTTPS = require('express-http-to-https').redirectToHTTPS
2020-03-16 22:55:32 +01:00
/* Files Imports & Variables */
2020-08-03 12:04:07 +02:00
const sequelize = require('./assets/utils/database')
const { PORT } = require('./assets/config/config')
const errorHandling = require('./assets/utils/errorHandling')
const isAuth = require('./middlewares/isAuth')
const isAdmin = require('./middlewares/isAdmin')
const app = express()
2020-03-16 22:55:32 +01:00
/* Middlewares */
2020-08-03 12:04:07 +02:00
app.use(helmet())
app.use(cors())
app.use(morgan('dev'))
app.use(express.json())
app.use(redirectToHTTPS([/localhost:(\d{4})/]))
2020-03-16 22:55:32 +01:00
2020-08-03 12:04:07 +02:00
/* Routes */
app.use('/images', express.static(path.join(__dirname, 'assets', 'images')))
app.use('/functions', require('./routes/functions'))
app.use('/categories', require('./routes/categories'))
app.use('/users', require('./routes/users'))
app.use('/admin', isAuth, isAdmin, require('./routes/admin'))
app.use('/favorites', require('./routes/favorites'))
app.use('/comments', require('./routes/comments'))
app.use('/quotes', require('./routes/quotes'))
app.use('/tasks', require('./routes/tasks'))
/* Errors Handling */
2020-08-03 14:14:45 +02:00
app.use((_req, _res, next) =>
errorHandling(next, { statusCode: 404, message: "La route n'existe pas!" })
) // 404
2020-03-19 22:59:06 +01:00
app.use((error, _req, res, _next) => {
2020-08-03 12:04:07 +02:00
console.log(error)
const { statusCode, message } = error
return res.status(statusCode || 500).json({ message })
})
2020-03-16 22:55:32 +01:00
/* Database Relations */
2020-08-03 12:04:07 +02:00
const Functions = require('./models/functions')
const Categories = require('./models/categories')
const Users = require('./models/users')
const Favorites = require('./models/favorites')
const Comments = require('./models/comments')
const Quotes = require('./models/quotes')
const Tasks = require('./models/tasks')
2020-04-06 23:06:21 +02:00
// A function has a category
2020-08-03 12:04:07 +02:00
Categories.hasOne(Functions, { constraints: true, onDelete: 'CASCADE' })
Functions.belongsTo(Categories)
2020-04-06 23:06:21 +02:00
// Users can have favorites functions
2020-08-03 12:04:07 +02:00
Users.hasMany(Favorites)
Favorites.belongsTo(Users, { constraints: false })
Functions.hasMany(Favorites)
Favorites.belongsTo(Functions, { constraints: false })
2020-04-06 23:06:21 +02:00
// Users can post comments on functions
2020-08-03 12:04:07 +02:00
Users.hasMany(Comments)
Comments.belongsTo(Users, { constraints: false })
Functions.hasMany(Comments)
Comments.belongsTo(Functions, { constraints: false })
2020-04-06 23:06:21 +02:00
// Users can suggest new quotes
2020-08-03 12:04:07 +02:00
Users.hasMany(Quotes)
Quotes.belongsTo(Users, { constraints: false })
2020-04-23 15:44:39 +02:00
// Users can have tasks
2020-08-03 12:04:07 +02:00
Users.hasMany(Tasks)
Tasks.belongsTo(Users, { constraints: false })
2020-04-23 15:44:39 +02:00
2020-03-16 22:55:32 +01:00
/* Server */
// sequelize.sync({ force: true })
2020-08-03 14:14:45 +02:00
sequelize
.sync()
2020-08-03 12:04:07 +02:00
.then(() => {
2020-08-03 14:14:45 +02:00
app.listen(PORT, () =>
console.log('\x1b[36m%s\x1b[0m', `Started on port ${PORT}.`)
)
2020-08-03 12:04:07 +02:00
})
2020-08-03 14:14:45 +02:00
.catch(error => console.log(error))