-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·47 lines (41 loc) · 1.79 KB
/
index.js
File metadata and controls
executable file
·47 lines (41 loc) · 1.79 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
//express is the framework we're going to use to handle requests
const express = require("express");
//Create a new instance of express
const app = express();
let middleware = require("./utilities/middleware");
const bodyParser = require("body-parser");
//This allows parsing of the body of POST requests, that are encoded in JSON
app.use(bodyParser.json());
//pg-promise is a postgres library that uses javascript promises
const pgp = require("pg-promise")();
//We have to set ssl usage to true for Heroku to accept our connection
pgp.pg.defaults.ssl = true;
app.use("/login", require("./routes/login.js"));
app.use("/register", require("./routes/register.js"));
/*
* Return HTML for the / end point.
* This is a nice location to document your web service API
* Create a web page in HTML/CSS and have this end point return it.
* Look up the node module 'fs' ex: require('fs');
*/
app.get("/", (req, res) => {
res.writeHead(200, { "Content-Type": "text/html" });
for (i = 1; i < 7; i++) {
//write a response to the client
res.write("<h" + i + ' style="color:blue">Hello World!!</h' + i + ">");
}
res.end(); //end the response
});
/*
* Heroku will assign a port you can use via the 'PORT' environment variable
* To accesss an environment variable, use process.env.<ENV>
* If there isn't an environment variable, process.env.PORT will be null (or undefined)
* If a value is 'falsy', i.e. null or undefined, javascript will evaluate the rest of the 'or'
* In this case, we assign the port to be 5000 if the PORT variable isn't set
* You can consider 'let port = process.env.PORT || 5000' to be equivalent to:
* let port; = process.env.PORT;
* if(port == null) {port = 5000}
*/
app.listen(process.env.PORT || 5000, () => {
console.log("Server up and running on port: " + (process.env.PORT || 5000));
});