forked from tigoe/NodeExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstaticPages.js
61 lines (51 loc) · 1.8 KB
/
staticPages.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
54
55
56
57
58
59
/*
Express.js Static pages example
Shows how to serve static pages along with dynamic routes
in Express.js 4.0
created 10 Feb 2015
by Tom Igoe
*/
var express = require('express'); // include express.js
var app = express(); // a local instance of it
app.use(express.static('public'));
// this runs after the server successfully starts:
function serverStart() {
var port = server.address().port;
console.log('Server listening on port '+ port);
}
// this is the callback function for when the client
// requests a static file:
function serveFiles(request, response) {
var options = { // options for serving files
root: __dirname + '/public/' // root is the /public directory in the app directory
};
// if there's an error sending a file, this function
// will be called:
function fileError(error) {
if (error) { // if there's an error
console.log(error); // log it locally
response.status(error.status) // and send a HTTP 404 status message
response.end(); // and close the connection
}
else { // if no error
console.log('Sent:', fileName); //log that you sent the file
}
}
// get the file name from the request parameter:
var fileName = request.params.name;
// send the file:
response.sendFile(fileName, options, fileError);
}
// 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:
response.write("Date: " + Date.now() + "\n");
response.end();
}
// start the server:
var server = app.listen(8080, serverStart);
// start the listeners for GET requests:
app.get('/files/:name', serveFiles); // GET handler for all static files
app.get('/date', serveDate); // GET handler for /date