-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.0-helloWorldWideWeb.js
51 lines (41 loc) · 1.42 KB
/
5.0-helloWorldWideWeb.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
// Modules vs. CommonJS
// require('modulename'); -- the old way.
//const http = require("http");
// const express = require('express');
// import {} from ''; == the new hotness
import express from 'express';
const app = express();
const port = 4242;
// let's declare some functions!
// a function we'll call when the server starts
function handleServerStart() {
console.log(`started server at ${port}`);
}
// a function that gets called to handle all the requests
// (not a very interesting web application)
function handleServerRequest(req, res, next) {
console.log("handle server request (middleware)");
// pass control to next link in the chain
next();
}
// this is still a function, declared anonymously
const getIndexPage = function (request, response) {
console.log(`Received request for '${request.url}' from agent ${request.headers['user-agent']}`);
response.statusCode = 200;
response.setHeader("Content-Type", "text/html");
response.end("Hello World\n");
}
// another function
const helloName = function (name) {
console.log(`Hello, ${name}}`);
}
console.log("Example function-object call:");
helloName("YourName");
// Express Middlware:
// this gets called on all requests
app.use(handleServerRequest);
// one Express Route
// a handler for a specific path
app.get('/', getIndexPage);
// if we don't listen, the program just wires up everything and then exits!
app.listen(port, handleServerStart);