-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress.js
More file actions
75 lines (58 loc) · 2.08 KB
/
express.js
File metadata and controls
75 lines (58 loc) · 2.08 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
68
69
70
71
72
73
74
75
//Express Frameowrk
//Express.js is a Node.js web application server framework, designed for building single-page, multi-page, and hybrid web applications.
//It is the de facto standard server framework for node.js. Frameworks built on Express.
//Requiring Express
const express = require("express");
const app = express();
//Middleware
app.use(express.json()); //Allows the use of middleware in the request pipeline
//REST - Representation State Transfer, Roy Fielding, METHODS below
const examples = [
{ id: 1, name: "Example Data One" },
{ id: 2, name: "Example Data Two" },
{ id: 3, name: "Example Data Three" }
];
//GET REQUESTS
app.get("/", (req, res) => {
res.send("Hello World");
});
app.get("/api/getExample", (req, res) => {
res.send([1, 2, 3, 4]);
});
app.get("/api/getExample/:id", (req, res) => {
//res.send(req.params.month) Returning specific param value
//res.send(req.params); // Returning entire param objects
let requestedExample = examples.find(
example => example.id === parseInt(req.params.id)
);
if (!requestedExample) {
res.status(404).send("The example was not found");
}
res.send(requestedExample);
});
app.get("/api/getExample/:month/:year", (req, res) => {
//Query String = ?key=value&key2=value2&key3=value3
res.send(req.query); // Returning entire query object
});
//POST REQUESTS
app.post("/api/postExample", (req, res) => {
const newExample = {
id: examples.length + 1, //Not an effective method for generating unique Ids
name: req.body.name
};
examples.push(newExample);
res.send(newExample);
});
//PUT REQUESTS
app.put("/api/putExample", (req, res) => {});
//DELETE REQUESTS
app.delete("/api/deleteExample", (req, res) => {});
// PORT - Environment Variable
const port = process.env.PORT || 3000; //Use environment variable or port 3000
//Listen on a Port
app.listen(port, () => {
console.log(`Listening on port + ${port}`);
});
console.log(process); // Process Global Variable
console.log(express); // Express Function
console.log(app); //Server is an EventEmitter