forked from ironhack-labs/lab-express-basic-auth
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathapp.js
More file actions
49 lines (39 loc) · 1.21 KB
/
app.js
File metadata and controls
49 lines (39 loc) · 1.21 KB
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
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const mongoose = require('mongoose');
const indexRouter = require('./routes/index');
const app = express();
mongoose.connect('mongodb://localhost/basic-auth', {
keepAlive: true,
useNewUrlParser: true,
reconnectTries: Number.MAX_VALUE
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
// -- 404 and error handler
// NOTE: requires a views/not-found.ejs template
app.use((req, res, next) => {
res.status(404);
res.render('not-found');
});
// NOTE: requires a views/error.ejs template
app.use((err, req, res, next) => {
// always log the error
console.error('ERROR', req.method, req.path, err);
// only render if the error ocurred before sending the response
if (!res.headersSent) {
res.status(500);
res.render('error');
}
});
module.exports = app;