-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
41 lines (32 loc) · 1.17 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
const express = require('express');
const favicon = require('serve-favicon');
const path = require('path');
const createError = require('http-errors');
const app = express();
const shorten = require("./routes/shorten");
const remove = require("./routes/remove");
const redirect = require("./routes/redirect");
app.use(express.json({}));
if(process.env.NODE_ENV !== 'production'){
const logger = require('morgan');
app.use(logger('dev'));
}
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
app.use("/",redirect);
app.use("/api",[shorten, remove]);
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
if (err.status === 404 ) return res.sendFile('/public/404.html', {root: __dirname });
if (err.status === 403 ) return res.sendFile('/public/403.html', {root: __dirname });
return res.sendFile('/public/404.html', {root: __dirname });
});
app.listen(process.env.PORT, () => {
console.log('listening on port ' + process.env.PORT);
});
module.exports = app;