This repository was archived by the owner on Jan 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
67 lines (52 loc) · 1.7 KB
/
server.js
File metadata and controls
67 lines (52 loc) · 1.7 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require("dotenv").config();
const mongoose = require("mongoose");
mongoose.Promise = global.Promise;
mongoose.connect(
process.env.MONGODB_URI,
{
useNewUrlParser: true
},
function(err) {
if (err) console.error("Could not connect to MongoDB.");
}
);
const http = require("http");
const WebSocket = require("ws");
const express = require("express");
const app = express();
const helmet = require("helmet");
app.use(helmet());
const compression = require("compression");
app.use(compression());
const bodyParser = require("body-parser");
app.use(bodyParser.json({ limit: "50mb" }));
// Initialization of modules
const User = require("./models/User");
const Thread = require("./models/Thread");
const Message = require("./models/Message");
app.get("/", function(req, res) {
res.send("Welcome to the React Native API.");
});
const cors = require("cors");
app.use("/api", cors());
// TODO: Add routes
const userRoutes = require("./routes/user.js");
const tchatRoutes = require("./routes/tchat.js");
app.use("/api", coreRoutes);
app.use("/api/user", userRoutes);
app.use("/api/tchat", tchatRoutes);
// tchat routes with WS
// TODO: add tchat routes using WS
// All HTTP methods (GET, POST, etc.) for pages that can't be found to display a 404 error
app.all("*", function(req, res) {
res.status(404).json({ error: "Not Found" });
});
appuse(function(err, req, res, next) {
if (res.statusCode === 200) res.status(400);
console.error(err);
// if (process.env.NODE_ENV === "production") err = "An error has occurred.";
res.json({ error: err });
});
server.listen(process.env.PORT, function() {
console.log(`React Native API is running on port ${process.env.PORT}`);
});