-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
37 lines (31 loc) · 1.31 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import express from 'express';
import next from 'next';
import { coloredMessage, colorCodes } from './src/api/library/functions';
import admin from './src/api/controllers/adminController';
import lixeira from './src/api/controllers/lixeiraController';
import logCapacity from './src/api/controllers/logCapacityController';
import user from './src/api/controllers/userController';
import cookieParser from 'cookie-parser';
import authOnly from './src/api/middlewares/authOnly';
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const nextapp = next({ dev })
const handle = nextapp.getRequestHandler()
nextapp.prepare().then(() => {
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use('/admin', admin);
app.use('/lixeira', lixeira);
app.use('/capacity', logCapacity);
app.use('/user', user);
app.all('*', authOnly(['/']), handle)
app.use(function (err, req, res, next) {
if (err.name === 'UnauthorizedError') res.status(401).cookie('from', req.originalUrl).redirect('/login');
})
app.listen(port, (err) => {
if (err) throw err
console.log(coloredMessage(`> Servidor iniciado em: http://localhost:${port}`, colorCodes.Green));
})
})