-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
49 lines (35 loc) · 1.13 KB
/
app.js
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
38
39
40
41
42
43
44
45
46
47
48
49
require('dotenv').config();
const express = require('express');
const requireEnv = require('./utils/requireEnv');
requireEnv(['DATABASE_URL', 'CLOUDINARY_URL', 'JWT_SECRET']);
const auth = require('./authentication/auth');
const api = require('./api');
const cors = require('./utils/cors');
const {
PORT: port = 3000,
HOST: host = '127.0.0.1',
} = process.env;
const app = express();
app.use(express.json());
// CORS til að geta gert ajax beiðnir frá öðrum serverum
app.use(cors);
app.use(auth);
app.use('/', api);
function notFoundHandler(req, res, next) { // eslint-disable-line
console.warn('Not found', req.originalUrl);
res.status(404).json({ error: 'Not found' });
}
function errorHandler(err, req, res, next) { // eslint-disable-line
console.error(err);
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
return res.status(400).json({ error: 'Invalid json' });
}
return res.status(500).json({ error: 'Internal server error' });
}
app.use(notFoundHandler);
app.use(errorHandler);
app.listen(port, () => {
if (host) {
console.info(`Server running at http://${host}:${port}/`);
}
});