-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
60 lines (47 loc) · 1.54 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
50
51
52
53
54
55
56
57
58
59
60
var cors = require("cors");
var express = require("express");
var cookieParser = require("cookie-parser");
var path = require("path");
var apiResponses = require("./helpers/api-response");
var indexRouter = require("./routes/index");
var apiRouter = require("./routes/api");
const { default: mongoose } = require("mongoose");
const Post = require("./models/post");
/**
* Connect to MongoDB
**/
var mongoDbUrl = process.env.MONGODB_URL || 'mongodb://localhost/photo-wall';
mongoose.connect(mongoDbUrl);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
global.__basedir = __dirname;
var app = express();
app.use(express.json());
app.use(cookieParser());
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// serve static files
app.use(express.static('public'))
// To Allow Cross-Origin Requests
app.use(cors());
//Route Prefixes
app.use("/api", apiRouter);
app.use("/", indexRouter);
app.use(express.json({ limit: "100mb" }));
app.use(express.urlencoded({ limit: "100mb" }));
app.use(express.urlencoded({ extended: false }));
//throw 404 if URL not found
app.all("*", function (req, res) {
return apiResponses.notFoundResponse(res, "Page not found");
});
app.use((err, req, res) => {
if (err.name === "UnauthorizedError") {
return apiResponses.unauthorizedResponse(res, err.message);
}
});
app.listen({ port: 5000 }, async () => {
console.log("Server up on http://localhost:5000");
});
app.maxHttpHeaderSize = 64 * 1024; // 64KB
module.exports = app;