-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathstaticPages.js
More file actions
43 lines (37 loc) · 1.24 KB
/
staticPages.js
File metadata and controls
43 lines (37 loc) · 1.24 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
/*
Express.js Static pages example
Shows how to serve static pages along with dynamic routes
in Express.js 4.0
created 10 Feb 2015
modified 4 Feb 2018
by Tom Igoe
*/
var express = require('express'); // include express.js
var server = express(); // a local instance of it
// serve static pages from public/ directory:
app.use('/',express.static('public'));
// this runs after the server successfully starts:
function serverStart() {
var port = this.address().port;
console.log('Server listening on port '+ port);
}
// this is the callback function for when the client
// requests the date (a dynamic route):
function serveDate(request, response) {
console.log('got a GET request');
// send the response:
var now = new Date();
response.send("Date: " + now + "\n");
response.end();
}
function serveHello(request, response) {
console.log('got a GET request');
// send the response, with request parameters:
response.send("Hello, " + request.params.name + " at " + request.ip);
response.end();
}
// start the server:
server.listen(process.env.PORT || 8080, serverStart);
// start the listeners for GET requests:
server.get('/date', serveDate); // GET handler for /date
server.get('/name/:name', serveHello); // GET handler for /name