This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (43 loc) · 1.61 KB
/
server.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
/**
* Filename: index.js
*
* The crux of our API; it defines all the libraries we use,
* as well as handles most of the initialization for our server.
*/
/* Modules */
const express = require('express');
/* API Middleware */
const mongoose = require('mongoose');
const cors = require('cors');
const bodyParser = require('body-parser');
/* Server instance */
const app = express();
/**
* Middleware for the API;
* Make sure to maintain this order when adding new ones;
* CORS should be the first one to be instantiated, then
* the bodyParser middlewares.
*/
app.use(cors({origin: '*'}));
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: false })); /* Parse x-www-form-urlencoded */
app.use(express.json());
/* HTML Rendering */
app.set('view engine', 'ejs');
/* Port that the server runs on; one is for production and other one for local testing. */
const port = process.env.PORT || 3000;
/* Database information */
const uri = process.env.MONGODB_URI;
//const uri = process.env.MONGODB_URI || fs.readFileSync(__dirname + "/uri.txt", "utf-8", (err) => console.log(err));
mongoose.connect(uri, {useUnifiedTopology: true, useNewUrlParser: true});
const connection = mongoose.connection;
/* Launch app and establish connection */
connection.once('open', () => {
console.log("Successfully connected to database.");
/* Import routes; these represent different endpoints that our API offers. */
const routes = require('./routes');
app.use('/api', routes);
app.listen(port, () => {
console.log(`API currently running on port ${port}`);
});
});